Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Previous assignment (Tutorial 0): solution to tutorial 0: Please write code in python programming language 1 Algorithms for High/Low Guessing Game In this problem, you

image text in transcribed

image text in transcribed

Previous assignment (Tutorial 0):

image text in transcribed

solution to tutorial 0:

image text in transcribed

Please write code in python programming language

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. 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 player's 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. 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 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...')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions