1. Engineering
  2. Computer Science
  3. previous assignment tutorial 0 solution to tutorial 0 please write...

Question: previous assignment tutorial 0 solution to tutorial 0 please write...

Question details


1 Algorithms for High/Low Guessing Game In this problem, you will implement and analyze two algorithms for playing the guessing game from Tutorial 0. In both algorithms, you must keep track of the lower () and upper (u) bounds and use these bounds to generate the next guess Gi+1). 1.a Random Guesses Perhaps the simplest strategy, you generate a random number between I, and u, which be- comes guess git Depending on the feedback, either too low or too high, you adjust the bounds and repeat. Hint: Start with uo 100 and lo 0. Let gi be the first guess. Likewise, let l and u be the updated bounds after the first guess Do the following: D Write pseudocode. Include both the guessing algorithm (the player) and the game rules (see Tutorial 0). Print out each guess instead of asking for keyboard input. □ Implement in Python. Make a function that takes the secret number as its input and returns the number of guesses. O Test your code with 50 as the secret number. 1.b Bisection A more intuitive strategy is the bisection algorithm. It cuts the search space in half each iteration using the guess gi (4 + ui)/2, where I and u, are the (updated) lower and upper bounds, respectively, after guess i. Do the following: O Write pseudocode. Include both the guessing algorithm (the player) and the game rules (see Tutorial 0). Print out each guess instead of asking for keyboard input.

□ Implement in Python. Make a function that takes the secret number as its input and returns the number of guesses. O Test your code with 50 as the secret number. 1.c Analysis Use your two functions, compare both strategies for the following secret numbers: 1,43, 76, 89. For each secret number value: O Play the game 100 times using the random strategy O Create a histogram showing the number of guesses to win with the random strategy O Compare the number of guesses with bisection strategy versus the average number of Create a histogram showing the guesses with the random strategy 1.d Discussion In a few sentences, interpret the results from both. Which strategy is better? Offer justifica- tion for why your results are not surprising.

Previous assignment (Tutorial 0):

Write pseudocode and Python program to interactively play the following game: 1. Generate a random integer number between 0 and 100. 2. Ask the player to guess a number. Capture their input from the keyboard. 3. Check if the number is valid (between 0 and 100, integer). If not, warn the player and goto Step 2. 4. If the players guess is correct, they win. Print out Congratulations and tell them how many guesses it took. Only count valid guesses. Terminate the program. 5 o lowit es to lo and goho 2 6. If the guess is too high, print Guess is too high. and goto Step 2 First write the pseudocode and then implement in Python. Test your program a few times. Submission Instructions: D Prepare your solutions in a Juypter Python notebook. Tip: Use Colaboratory D Embed a photo/scanned image of your handwritten pseudocode. Add extensive comments to your Python code.

solution to tutorial 0:

1 # import libraries 2 import numpy as np [ ] 1 # function to accept guess from user and determine if guess matches secret 2 # number between 0 and 100 3 def guessing_game (rand num): Play the guessing game. User inputs guesses with keyboard input. Program gives hints too low or too high. Arguments: 9 10 rand_num: the secret number (must be between 0 and 100) Returns: nothing 12 13 14 15 16 17 18 19 20 21 # flag for 100 flag = False # counter for number of guesses ctr 0 # while loop to guess for number till guess is correct while not flag: # input from user. convert from a string to floating point number guess -float (input( Guess the number: ) ) 23 24 25 26 27 28 29 30 31 32 # check if number is within suggested range and is an integer if guess >= 0 and guess <=100 and guess.is..integer(): ctr +-1 # check if guess is correct print(nCongratulations! Your guess is correct. Number is:,rand num) print(Number of guesses:,ctr) 34 35 36 37 38 39 40 41 4 2 43 # flag set to True to terminate loop flagTrue # check if guess overshot number elif guess > rand num: print(Guess is too high) # check if guess undershot number else: print(Guess is too low) 45 46 47 48 49 # if number outside suggested range, ask user for new guess else: print(Invalid guess. Try again...)

Please write code in python programming language

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution