| CIT 591 Midterm Exam, Fall 2010 | Name ______________________________ |
Please keep all answers short and to the point. Do not add information that is not asked for; you will not gain points, but you may lose points if you get it wrong
| def f1(ints): # ints is a list of integers n = 0 for i in range(1, len(ints)): if ints[i] > ints[n]: n = i return n |
| def f2(ints): # ints is a list of integers for i in range(1, len(ints)): if ints[i] < ints[i - 1]: return False return True |
| def f3(s): # s is a string count = 0 for i in range(1, len(s)): if s[i] == s[i - 1]: count += 1 return count |
| def f4(ints): # ints is a list of integers flag = False for i in range(0, len(ints)): flag = flag or (ints[i] < 100) return flag |
| def f5(s): # s is a string containing words and spaces count = 0 if s[0].isalpha(): count = 1 for i in range(1, len(s)): if s[i - 1] == " " and s[i].isalpha(): count += 1 return count |
| def f6(n): # n is a positive integer k = 1 while n * k <= 100: k += 1 return n * k |
| def f7(s): # s is a string global index for index in range(0, len(s)): return s[index] |
s is a string, then s[i].alpha() tests whether the character at location i in the string contains a letter. True if there is at least one letter in the string. The first line has been written for you. def contains_some_letters(s):
False if there any non-letters in the string. The first line has been written for you. def contains_only_letters(s):
unittest module. (Just list them, don't tell what they do.)global statement inside a function? (Be precise.) __init__ method?__str__ method?
def my_reverse(lst):
"""Makes a copy of the lst, but in reverse order."""
copy = lst
for i in range(0, len(lst)):
copy[i] = lst[len(lst) - i]
return copy
- (5 points) What will be returned by the call
my_reverse([1, 2, 3, 4, 5])?
- (5 points) What do you need to do to correct the method?
nickname to make it easy to look up these nicknames.