Compsci 06/101, Spring 2011, Feb 14

Name____________________   net-id _________       Name____________________    net-id _________

Name____________________   net-id _________       Name____________________    net-id _________

Yahtzee/Scoreit

In lab last week the following code 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
  1. 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)]
      

  2. 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])
      

    ClassScores

  3. In Lab suggestions were made for finding the mode of the values stored in a list of scores for the ClassScores APT. In this example scores is a list of int values all between 0 and 100, inclusive.

    Which of the following correctly sets mode to be the number of times the most frequently occurring value occurs. Note: not the modal value, but it's number of occurrences. There could be more than one correct answer.

    1.     mode = max([scores.count(i) for i in scores])
      
    2.     mode = max([scores.count(i) for i in range(0,101)])
      
    3.     mode = max([val for val in scores])
      

GuessNumber

These questions are based on the code GuessNumber.py

  1. Which of the following is true about the variables answer and guess and their respective values assigned before the loops in the functions play_game_computerguess and play_game_humanguess, respectively?

    1. The initializations ensure that each loop will be entered the first time -- the actual values used: 'none' and -1 ensure the loops will be entered.

    2. Python requires initializing variables and any values could have been used, there's nothing special about 'none' and -1 that are used.

    3. It would be better to ask the user to initialize the variables before the loop using raw_input.

  2. About how many guesses will be needed by the computer to guess the user's secret number when it's between 1 and 1000?
    1. 10
    2. 100
    3. 512

  3. In the loop-body of function play_game_humanguess there's no code to process the return value "correct" returned by get_status. Which is the best explanation for why no code is needed to process this return value?

    1. When there's no elif or else to handle a case Python will automatically generate code to handle it.

    2. The loop guard: secret != guess handles the case of a correct guess and it's the code executed after the elif.

    3. Because there's no code for "correct" this means that the number of guesses reported is wrong after the loop finishes.