I tried my best to list the topics for the exam but I may have forgotten one or two. Your default assumption should be that if I covered it in class before design, it is fair game for the exam.
I will add practice problems as I find new interesting questions, so come back to this page every few days, more practice problems may have appeared.
In addition to all concepts from Test 3,
Files
- Opening files in read or write mode, and closing files
- Reading a line from a file, if necessary splitting it into words and iterating over the words
- Reading a file with a for loop when the number of lines is known, reading it with a sentinel loop when number of lines is unknown
- Writing to files
Functions
- Goal and Syntax
- Passing a value as an input parameter vs asking the user for an input
- What happens when a function is called
- Stack Diagrams
- Receiving one or many values from a function that was called, returning one or many values in a function
- Memory representation of lists, its effect on functions and stack diagrams
- Parameter passed by value vs passed by reference
Practice Problems
- Finish any lab exercises that you did not have time to complete during lab time. Remember, I added a whole page of exercise for the week of Fall break during which we did not have lab.
- Write a program that asks the user for the name of an input file, assumes that the file contains a single column of numbers, and prints on screen the last number of the file. The program should assume that there will be number in the file until
readline
returns an empty line
- Write a program that asks the user for the name of an output file, then asks the user for numbers until the users enters a blank line. All the numbers should be written in the output file in a single column, one number per line.
- Write a function called
evenize
that takes a list of numbers as input and modifies the elements of the list so that all the elements that are even are unchanged, but all odd elements are multiplied by 2.
- Write a function called
extractEvens
that takes a list of numbers as input and returns a new list that contains all the even elements of the input list in the same order in which they apppeared in the original list.
- Write a function that takes a list of numbers and a filename as input parameters and write all the numbers of the list in a file named after the second parameter. The numbers should be arranged in 3 neat columns and the numbers should be left aligned. You can assume that all the numbers will be between -999999999 and 999999999. The last line is allowed to contain fewer than 3 numbers, but the numbers should still be in up to 3 columns, left aligned, and the last elements of the list should be written from left to right.
For example, if the list contains the 5 elements [1000000, -59, 3, 34652, 1234543, 1729]
, then the file should look as follows:
1000000 -59 3
34652 1234543
- Determine what would be printed by each of the following pieces of code:
def printLots(n):
for i in range(n):
print("{0:$<5} {1:*>5} {2:!^4}".format(i, i+1, i+2))
def addToList(x,theList):
for i in range(x):
theList.append(x)
myList = [1,2,3,4,5]
addToList(6, myList)
print(myList)
def doSomething(theList):
newList = []
for i in range(len(theList)):
newList.append(theList[i] + 5)
return newList
myList = [1,2,3,4,5]
aList = doSometyhing(myList)
print(myList, aList)
- Draw a stack diagram at the point indicated by a comment in the code, and determine what would be printed by the program.
def main():
myList = [1,3,5,78,9]
otherList = doSomethingElse(myList)
print(otherList)
def doSomethingElse(aList):
for i in range(len(aList)-1):
aList[i] = aList[i] + 3
# draw stack diagram here
return aList * 2
if __name__ == "__main__":
main()