ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: guessing
Submitter: max baseman (other recipes)
Last Updated: 2006/10/05
Version no: 1.0
Category:

 

Not Rated yet


Description:

it includes two guessing flavors one that randomly guesses a number and one that guesses in the most efective way i could think of (my brother had the second idea)

Source: Text Source

def number(number):
    ran=input("range  >")
    ran=ran+1
    from random import randrange
    guessed=[]
    guess=randrange(ran)
    print guess
    guessed.append(guess)
    guesses=1
    guessed=[]
    while guess !=number:
        guess=randrange(ran)
        if guess not in guessed:
            guessed.append(guess)
            guesses=guesses+1
            print guess
        
    print"i got the number",number,"in",guesses,"guesses"
        
    
        
def num(number):
    r=input("range >")+1
    if r<number:
        r=number+1
    guess=r/2
    print guess
    guesses=1
    min = 0
    max = r
    while guess!=number:
        if guess < number:
            min = guess
        else:
            max = guess
        guess = (min + max) / 2
        guesses += 1
        print guess
    print "i got the number",number,"in",guesses,"guesses"

Discussion:

this is a fun program that can waste time or just be usefull on some sort of research



Add comment

No comments.



Highest rated recipes:

1. A simple XML-RPC server

2. Web service accessible ...

3. IPy Notify

4. Changing return value ...

5. Quantum Superposition

6. Pickle objects under ...

7. Generalized delegates ...

8. Reorder a sequence (uses ...

9. Setting Win32 System ...

10. ObjectMerger




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.