Comp 115 Test 4 Study Guide

Test on November 12-13 (depending on your lab section) 3:30-4:20 in lab

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

Functions

Practice Problems

  1. 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.
  2. 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
  3. 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.
  4. 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.
  5. 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.
  6. 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   
      
  7. Determine what would be printed by each of the following pieces of code:
    1.   def printLots(n):
            for i in range(n):
                print("{0:$<5} {1:*>5} {2:!^4}".format(i, i+1, i+2))
      
    2.   def addToList(x,theList):
            for i in range(x):
                theList.append(x)
      
        myList = [1,2,3,4,5]
        addToList(6, myList)
        print(myList)
      
    3.   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)
      
      
  8. 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()