RE: Python: Help With Beginner Stuff

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Fri, 15 Oct 2010 15:34:03 -0400

See the following:

This rather simple game begins something like "I'm thinking of an integer 
between forty and sixty inclusive, and to your guesses I'll respond 'High', 
'Low', or 'Yes!' as might be the case." Supposing that N is the number of 
possible values (here, twenty-one as "inclusive" was stated), then at most  
questions are required to determine the number, since each question halves the 
search space. Note that one less question (iteration) is required than for the 
general algorithm, since the number is already constrained to be within a 
particular range.

Even if the number we're guessing can be arbitrarily large, in which case there 
is no upper bound N, we can still find the number in at most  steps (where k is 
the (unknown) selected number) by first finding an upper bound by repeated 
doubling.[citation needed] For example, if the number were 11, we could use the 
following sequence of guesses to find it: 1, 2, 4, 8, 16, 12, 10, 11

One could also extend the technique to include negative numbers; for example 
the following guesses could be used to find −13: 0, −1, −2, −4, −8, −16, −12, 
−14, −13.
 
For more, go to:

http://en.wikipedia.org/wiki/Binary_search_algorithm

Take care,
Sina

________________________________

From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: Friday, October 15, 2010 3:28 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: Python: Help With Beginner Stuff



Hi,

If I was guessing, I'd pick 50 first. Then if it told me that I was too low, 
I'd pick 75. If I was too high, I'd pick 25. I used to know some sort of 
formula for expressing how to pick a number like that. If I coded it, I'd want 
it to work no matter what the low number was, and no matter what the high 
number was. Math is not my best friend, though. I think I saw a book about 
beginning math for game programmers somewhere. I think it was in the Jamal Misc 
programming docs.

 

Jim

 

Jim Homme,

Usability Services,

Phone: 412-544-1810. Skype: jim.homme

Internal recipients,  Read my accessibility blog 
<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx> . Discuss 
accessibility here 
<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx> . 
Accessibility Wiki: Breaking news and accessibility advice 
<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>
 

 

From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of R Dinger
Sent: Friday, October 15, 2010 2:46 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: Python: Help With Beginner Stuff

 

Hi Jim,

 

Your interval bisection program should take 7 guesses max.  I will take a quick 
look, but I have to go out soon and someone else may answer in the interim.  
And there is a bisect (or something) module in Python.

 

Richard

        ----- Original Message ----- 

        From: Homme, James <mailto:james.homme@xxxxxxxxxxxx>  

        To: programmingblind@xxxxxxxxxxxxx 

        Sent: Friday, October 15, 2010 11:19 AM

        Subject: Python: Help With Beginner Stuff

         

        Hi,

        I know more than what I'm showing in this program, but I'm taking my 
learning back so as not to miss anything crucial. The book I'm reading is in 
the Jamal collection of Python docs. It's called "Python Programming for the 
absolute beginner." I'm doing the Chapter 3 assignment. The assignment was to 
write pseudocode for a program that would simulate the computer guessing a 
number from 1 to 100. I wrote the pseudocode. That made me confident enough to 
take a stab at writing the program. What follows is my program and one set of 
output from it. My question is this. It seems like the way I have worked it 
out, the computer is always guessing 10 or 11 times to get the answer. How can 
I make the simulation a little more realistic?

         

        === begin program ===

         

        # comp_guess.py

        # By Jim Homme

        # October 15, 2010

        # Simulate the computer guessing a number between 1 and 100 until it 
guesses correctly.

         

        import random

         

        lower = 1

        upper = 100

        answer = int(raw_input("Give me a number between 1 and 100 for the 
computer to guess: "))

        print "\nThe answer is %d." % (answer)

        comp_guess = random.randint(lower, upper) # The computer makes a guess

        guesses = 1

        print "For guess %d, the computer guessed %d." % (guesses, comp_guess)

        while comp_guess != answer:

          if comp_guess < answer:

            print "The computer guessed too low."

            lower  = comp_guess +1 # So it won't guess the same number again.

            print "lower is now %d." % (lower)

          else:

            print "The guess is too high."

            upper = comp_guess -1 # So it won't guess the number again.

            print "upper is now %d." % (upper)

          guesses += 1

          comp_guess = random.randint(lower, upper) # Guess again.

         

          print "For guess %d, the computer guessed %d." % (guesses, 
comp_guess) # The final guess.

        print "The computer guessed the number in %d guesses" % (guesses)

        print "Can you beat that?"

        raw_input("Press Enter to quit")

         

        === End program ===

         

        === Begin output ===

         

        Give me a number between 1 and 100 for the computer to guess: 

        The answer is 44.

        For guess 1, the computer guessed 89.

        The guess is too high.

        upper is now 88.

        For guess 2, the computer guessed 35.

        The computer guessed too low.

        lower is now 36.

        For guess 3, the computer guessed 82.

        The guess is too high.

        upper is now 81.

        For guess 4, the computer guessed 37.

        The computer guessed too low.

        lower is now 38.

        For guess 5, the computer guessed 65.

        The guess is too high.

        upper is now 64.

        For guess 6, the computer guessed 52.

        The guess is too high.

        upper is now 51.

        For guess 7, the computer guessed 41.

        The computer guessed too low.

        lower is now 42.

        For guess 8, the computer guessed 48.

        The guess is too high.

        upper is now 47.

        For guess 9, the computer guessed 47.

        The guess is too high.

        upper is now 46.

        For guess 10, the computer guessed 43.

        The computer guessed too low.

        lower is now 44.

        For guess 11, the computer guessed 44.

        The computer guessed the number in 11 guesses

        Can you beat that?

        Press Enter to quit

         

        === End Output ===

         

         

         

         

         

         

        Jim Homme,

        Usability Services,

        Phone: 412-544-1810. Skype: jim.homme

        Internal recipients,  Read my accessibility blog 
<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx> . Discuss 
accessibility here 
<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx> . 
Accessibility Wiki: Breaking news and accessibility advice 
<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>
 

         

         

        ________________________________

                This e-mail and any attachments to it are confidential and are 
intended solely for use of the individual or entity to whom they are addressed. 
If you have received this e-mail in error, please notify the sender immediately 
and then delete it. If you are not the intended recipient, you must not keep, 
use, disclose, copy or distribute this e-mail without the author's prior 
permission. The views expressed in this e-mail message do not necessarily 
represent the views of Highmark Inc., its subsidiaries, or affiliates.


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: