Important Questions For Final Exam
Question 1.
def both_start_with(s1, s2, prefix)
'''(str, str, str) -> bool
Return True if and only if s1 and s2 both start with the letters in prefix.
'''
if s1.startswith(prefix) and s2.startswith(prefix):
return True
else:
return False
The function works, but the if statement can be replaced with a single return statement:
return # CODE MISSING HERE
Write the missing expression. Use only the parameters, two calls on method startswith, and operator and.
Do not use unnecessary parentheses: you need them for the method calls, but nothing else. Do not include the word return; just write the expression.
Question 2.
def gather_every_nth(L, n):
'''(list, int) -> list
Return a new list containing every n'th element in L, starting at index 0.
Precondition: n >= 1
>>> gather_every_nth([0, 1, 2, 3, 4, 5], 3)
[0, 3]
>>> gather_every_nth(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], 2)
['a', 'c', 'e', 'g', 'i']
'''
result = []
i = 0
while i < len(L):
result.append(L[i])
i = # CODE MISSING HERE
return result
MORE QUESTIONS AND THEIR ANSWERS WOULD BE POSTED ASAP, BE UPDATED!!
whr are answers
ReplyDelete