import random 2 3 def 4 guess_winner(players=('Amy', 'Bill', 'Cathy', 'Dale')): 5 '''Summarize the function in this docstring. 6 7 Provide descriptions for the arguments and say what type each one is. 8 Describe the type and meaning of the returned. 9 ''' 10 winner = random.choice(players) 11 12 #### 13 # Summarize the following section of code here 14 #### ',end='') 15 print('Guess which of these people won the lottery: 16 for p in players[:len(players)-1]: # explain index here 17 ', end='') 18 print(players[len(players)-1]) # explain this line here 19 20 #### 21 # Summarize the following section of code here 22 #### 23 guesses = 1 24 while raw_input() != winner: print('Guess 25 again!') 26 guesses += 1 print('You guesses, 27 guessed in', 'guesses!') 28 return guesses It is often useful to consider how many times a loop will execute. If while is called with a list of 100 options, how many times guess_game(options) might the user have to guess? Define a function that implements a number guessing game as goguess() described below. Strategize as pair programmers. Then iteratively code and test. In []: goguess() I have a number between 1 and 20 inclusive. Guess: 6 6 is too low. Guess: 10 10 is too high. Guess: 9 Right! My number is 9! You guessed in 3 guesses! If you change from the previous program to between 1 and 20 between 1 and how many guesses will you need to guarantee that you have the right 6000, answer? Explain. Sorting and searching are two important algorithmic problems. You can compare different sorting algorithms (insertion sort, selection sort, bubble sort, etc.) at . Which algorithm is fastest when a list is nearly sorted? Compare the algorithms empirically as shown below. The most important solutions to the problem of searching are linear search (checking one at a time, potentially going through a whole list) and binary search
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import random def guess_winner(players=('Amy', 'Bill', 'Cathy', 'Dale')): '''Summarize the function in this docstring. Provide descriptions for the arguments and say what type each one is. Describe the type and meaning of the returned. ''' winner = random.choice(players) #### # Summarize the following section of code here #### print('Guess which of these people won the lottery: ',end='') for p in players[:len(players)-1]: # explain index here ', end='') print(players[len(players)-1]) # explain this line here #### # Summarize the following section of code here #### guesses = 1 while raw_input() != winner: print('Guess again!') guesses += 1 print('You guessed in', guesses, 'guesses!') return guesses It is often useful to consider how many times a while loop will execute. If guess_game(options) is called with a list of 100 options, how many times might the user have to guess? Define a function goguess() that implements a number guessing game as described below. Strategize as pair programmers. Then iteratively code and test. In []: goguess() I have a number between 1 and 20 inclusive. Guess: 6 6 is too low. Guess: 10 10 is too high. Guess: 9 Right! My number is 9! You guessed in 3 guesses! If you change between 1 and 20 from the previous program to between 1 and 6000, how many guesses will you need to guarantee that you have the right answer? Explain. Sorting and searching are two important algorithmic problems. You can compare different sorting algorithms (insertion sort, selection sort, bubble sort, etc.) at . Which algorithm is fastest when a list is nearly sorted? Compare the algorithms empirically as shown below. The most important solutions to the problem of searching are linear search (checking one at a time, potentially going through a whole list) and binary search
1.3.8.a_whileloops.docx