Tasks week 40 Flashcards

(5 cards)

1
Q

Write a function palindrome(s) that checks if the given string s is a palin-
drome or not

A

def palindrome(s):
if list(s) == list(s)[::-1] :
print (“string is a palindrome”)
else:
print (“string is not a palindrome”)
palindrome(“racecar”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a function palindrome(s) that checks if the given string s is a palin-
drome or not

A
def palindrome(s):
    s_reversed = reversed(s)
    if list(s) == list(s_reversed):
        print('This is a palindrome')
    else:
        print('This is not a plindrome')

palindrome(‘madam’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a function bubblesort(l) that sorts a given list of numbers L using
Bubble sort.2 The sorting is done in-place, which means that only L is used
and no copies or new lists.

INIT variable change as True
WHILE change is True DO:
SET change to False
FOR each item in L at position i except the last item DO:
IF item at position i is larger than item at position i+1:
exchange items
SET change to True

A
l = [9,8,7,6,5,4,3,10,1,0]
def bubblesort(l):
    change = True
    while change :
        change = False 
        for i in range(len(l)-1) :
            if l[i] > l[i+1] : 
                l[i], l[i+1] = l[i+1], l[i] #tjekker de første to først også bytter den rundt 
                change = True

print (l)
bubblesort(l)
print (l)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
Extend the module quiz with the following functionality to allow for user-
generated questions:
userQuestion(): When calling the function, input dialogs are used to
type in (1) the quiz question, (2) four possible answers, and (3) the correct
answer (1 to 4). 
Make sure that the user inputs a valid number. All information is stored to a local CSV le by calling the store function.
A
def userQuestion():
    q = input("Question: ")
    answers = []
    for i in range(4):
        answers.append(input(str(len(answers)+1)+". Answer: "))
    valid = False
    while not valid:
        correct =  int(input("What is the correct answer? Type a number in range 1 to "+str(len(answers))+": "))
        if correct > 0 and correct <= len(answers):
            valid = True
    addQuestion(q,answers,correct)
    storeQuestion(q,answers,correct)
userQuestion()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Store(question,answers,correct): The function takes the question as a string, the four answers as a list of strings and the correct answer as an integer value in the range [1,4].

It opens the local the local file my questions.csv, and adds the following line:
question \t answer1 \t answer2 \t answer3 \t answer4 \t correct

Finally, the question is added to the global list of quiz questions questions by calling the add function.

A
def storeQuestion(question, answers, correct):
    file = open("myqlist.csv","a")
    line = question 
    for a in answers:
        line += a
    line += str(correct)
    file.write(line)
    file.write("\t")
    file.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly