| CIT 591 Midterm Exam, Fall 2011 | 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. If the question asks for a statement, write a statement, not a function. If the question asks for a function, write a function, not a class.
score is supposed to be between 0 and 100, inclusive. Write a statement or statements (not a method) to set score to zero if it is negative, or to 100 if it is larger than 100.aList is a list of integers.Write a statement or statements (not a method) to print out the first negative number in aList. If there are no negative values, don't print anything.None.powers to a list containing the first 100 powers of 2 (that is, [1, 2, 4, 8, 16, 32, ...]). | a. | def firstBad(aList):
# aList is a list of integers
for i in range(0, len(aList)):
if aList[i] < 0 or aList[i] > 100:
return i
return -1
|
|
| b. | def buzz(anInt):
# anInt is a positive integer
if anInt % 7 == 0 or '7' in str(anInt):
return 'buzz'
else:
return anInt
|
|
| c. | def flip(aList):
# aList is a list of numbers
x = aList[:]
for i in range(1, len(aList)):
if x[i] < x[i - 1]:
x[i], x[i - 1] = x[i - 1], x[i]
return x[-1]
|
|
| d. | def mapEven(aList):
# aList is a list of positive integers
even = map(lambda x: x % 2 == 0
return list(even, aList))
|
|
| e. | def filterEven(aList):
# aList is a list of positive integers
f = lambda x: x % 2 == 0
return list(filter(f, aList))
|
|
| f. | import functools
def reduceRunning(aList):
# aList is a list of floating point numbers
f = lambda x, y: (x + y) / 2
return functools.reduce(f, aList)
|
(Tell generally what this function does; you don't have to be numerically exact.) |
largest takes a list of integers as a parameter, and returns the largest number in the list (or None if the list is empty). Write a unit test method to test largest.largest takes a list of floating point numbers instead of integers. (You can rewrite the entire method if that's easiest, or just tell what changes need to be made.)def factorial(n):
return n * factorial(n - 1)
cardValues to hold this information.foo you see the statement print(bar). How do you figure out whether bar is local or global? Give a rule or rules for determining this. testScores contains integers in the range 0 to 100; but since some people didn't take the test, the list may also contain some None values. Write a statement or statements (not a complete method) to remove those None values from the list. For full credit, do this in the fewest possible number of statements.self? Or, to put the question another way, what is special about this particular value that isn't true about any other variable in the method?