Comp 115 Test 4 Study Guide

Test on April 22 Eastern time

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 files, 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.

If you want to see the short video I will record about the test, click here (video is now available!).

In addition to all concepts from Test 3,

Lists

Repetition

Files

(Note: questions about files will not be too hard, I know you do not have a whole lot of practice with this)

Practice Problems

  1. 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.
  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.
  3. Determine what would be printed by each of the following pieces of code, and make a table of the values that each variable successively takes while the code is being executed:
    1. Assume the user enters the number 30 while running the following:
        myList = [5, 17, 45, 30, 29, 18]
        lookFor = int(input("Enter a number: "))
        i = 0
        while i < len(myList) and myList[i] != lookFor:
            i = i + 1
        print(i)
    2. What would be printed by the program of the previous question if the user entered 19.
    3.   for i in range(5):
            print("*"*i)
    4.   for i in range(5):
            print("{0:_>5}".format("*"*i))
    5.   myList = [1,2,3,4,5]
        for i in range(len(myList)):
            if myList[i] == 4:
                myList[i] = 1729
        print(myList)
    6.   currentValue = 2
        while currentValue < 100:
            print(currentValue)
            currentValue = 2 * currentValue + 5
            print("DONE")
    7.   def printLots(n):
            for i in range(n):
                print("{0:$<5} {1:*>5} {2:!^4}".format(i, i+1, i+2))
      
    8.   def addToList(x,theList):
            for i in range(x):
                theList.append(x)
      
        myList = [1,2,3,4,5]
        addToList(6, myList)
        print(myList)
      
    9.   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)
      
      
  4. 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()
    
  5. Practice questions on files TBA