Compsci 06/101, Spring 2012, February 7

By entering your name/net-id below you indicate you are in class on February 7 to answer these questions and that you have answered them. Your name should not appear unless you are in class when answering these questions.
Name_________________   net-id _______   Name_________________   net-id _______


Name_________________   net-id _______   Name_________________   net-id _______

  1. What is the value of the string variable st when the code below executes? Explain (to yourself or the class, you don't have to write it down) your answer.
      word = "creation"
      st = ""
      for i in range(0,len(word)):
          if i % 2 == 0:
              st = st + word[i]
    
      print st   # value here?
    
    1. "ceto"
    2. "rain"
    3. "ratio"

  2. A five letter word is printed by the code below, what is it?
      words = ["self", "contained", "underwater", "breathing", "apparatus"]
      abb = ""
      for w in words:
          abb = abb + w[1]
      print abb  
    
    
    1. "scuba"
    2. "eonrp"
    3. "secounbrap"


    The Yahtzee/ScoreIt APT was suggested for the Yahtzee/Scoreit APT in which you determine the maximal score in the "above the line" section of a Yahtzee game. The idea is expressed in the code below to process the list toss, where toss has five-values 1-6 representing die rolls. For purposes of this exposition assume toss=[3,2,5,3,5] -- the first step is to count the number of occurrences of each possible die-roll.

    toss = [3,2,5,3,5] counts = [0,1,2,0,2,0] # what expression creates this? # missing code here
  3. Which of the following is the correct list comprehension for counts?
    1.    counts = [toss.count(i) for i in toss]
      
    2.    counts = [toss.count(i) for i in range(1,7)]
      
    3.    counts = [toss[i] for i in range(1,7)]
      

  4. Which of the following is the correct list comprehension so that the body of the function is one line using toss as shown:

    1.    return max([i*toss.count(i) for i in range(1,7)])
      
    2.    return max([toss[i]*toss.count(i) for i in range(1,7)])
      
    3.    return max([i*toss[i] for i in range(1,7)])
      
    4.    return max([i*toss.count(i) for i in toss])