Commit c83d10d3 authored by mirree's avatar mirree
Browse files

Add tasks and solutions to Inspera exercise 2

parent c2f598e2
# Del 1
def input_strings():
lst = []
string = ""
for i in range(4):
string = input("Skriv inn en streng: ")
lst.append(string)
return lst
# Del 2
def acronym():
lst = input_strings()
for string in lst:
print(string[0].upper(), end='')
# Del 1
def derivation(value,f):
h = 1e-8
limit = (f(value+h)-f(value))/h
return round(limit,3)
# Del 2
def polynom(x):
return x**2+2*x+13
print(derivation(3,polynom))
# Del 1
def smallify_words(objects):
new_objects = []
for word in objects:
new_objects.append(word.lower())
return new_objects
# Del 2
def get_five_objects():
while True:
objects = (input("Enter five objects separated by ';': ").split(';'))
if len(objects) == 5:
return smallify_words(objects)
print("You were supposed to enter FIVE objects, not", str(len(objects)) + '. Try again.')
# Del 3
def play_game():
objects = smallify_words(get_five_objects())
print("""We shall now play a little memory game. Can you remember all the words you gave me?
If it gets to hard for you and you want to give up, write 'quit'""")
word = input("What is your guess? ").lower()
while word != 'quit':
if word in objects:
objects.remove(word)
print("Congratulations! You remembered", word)
else:
print("Sorry, that was not one of the words")
if not len(objects):
print("You did it! You remembered all the objects")
break
word = input("What is your next guess? ").lower()
# play_game()
# Del 1
import random
songs = [("You hear the door slam. And realize there's nowhere left to", "run"),
("Oh, I wanna dance with somebody. I wanna feel the", "heat"),
("There's a fire starting in my heart. Reaching a fever", "pitch"),
("Hey, I just met you and this is crazy. But here's my", "number"),
("'Cause baby, you're a firework. Come on, show 'em what you're", "worth")]
def pop_random_song(songs: list):
index = random.randint(0, len(songs)-1)
return songs.pop(index)
# Del 2
def get_guess():
return input("What is the next word? ")
def song_contest(songs):
while True:
song = pop_random_song(songs)
print("The lyrics are:")
print(song[0])
guess = get_guess()
while guess.lower() != song[1]:
print("Wrong guess. Try again.")
guess = get_guess()
print("Correct!")
if not len(songs):
print("Congratulation, music lover! You have managed to get every song lyric we had correct!")
break
one_more = input("Do you want to go again? (y/n) ")
if one_more.lower() == 'n':
print("Welcome back later :D")
break
# song_contest(songs)
# Del 1
def do_user_like(items):
scores = []
print("On a scale from 1 to 10 where 10 is the highest, how much do you like: ")
for item in items:
val = int(input(item + "? "))
while not 0 < val <= 10:
print("You have to give a value in the interval [1, 10]. Try again.")
val = int(input(item + "? "))
score = (item, val)
scores.append(score)
return scores
# Del 2
def copy_list(lst):
copy = []
for i in range(len(lst)):
copy.append(lst[i])
return copy
def get_prioritized_list(scores):
pri_list = copy_list(scores)
i = 1
while i < len(pri_list):
j = i
while j > 0 and (pri_list[j-1][1] < pri_list[j][1] or (pri_list[j-1][1] == pri_list[j][1] and pri_list[j-1][0] > pri_list[j][0])):
hold_score = pri_list[j]
pri_list[j] = pri_list[j-1]
pri_list[j-1] = hold_score
j -= 1
i += 1
return pri_list
# Del 3
def what_user_likes_best(items, num):
if num < 1 or num > len(items):
print("Invalid number given.")
return
scores = do_user_like(items)
pri_lst = get_prioritized_list(scores)
if num == 1:
print("Your number", num, "is: ")
else:
print("Your top", num, "are: ")
for i in range(num):
print(str(i+1)+".", pri_lst[i][0])
# what_user_likes_best(['Dogs', 'Cats', 'Chocolate', 'Pancakes', 'Ice cream'], 5)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment