Chapter 10: Program Organization

Example on Page 219

In [1]:
def sum_digits(n):
    sum = 0 #local variable
    while(n>0):
        sum =sum+(n%10)
        n=n/10
    return sum
#sum_digits(10)
Out[1]:
1

Example on Page 222

In [4]:
STACK_SIZE=100
contents=[None]*STACK_SIZE
top=0
def make_empty():
    top=0
def is_empty():
    return top==0
def is_full():
    return top==STACK_SIZE
def push(i):
    if(is_full()):
        call_stackoverflow=0
        #stack_overflow()
    else:
        contents[top+1]=i
def pop():
    if(is_empty()):
        call_stackoverflow=0
        #stack_underflow()
    else:
        return contents[top-1]

Example guess.c, Page 224

In [2]:
import random
MAX_NUMBER=100
#def initialize_number_generator():
def choose_new_secret_number():
    secret_number=random.randint(1,MAX_NUMBER) #generate random number
    return secret_number
def read_guesses():
    num_guesses=0
    while(1):
        num_guesses=num_guesses+1
        guess=int(raw_input("Enter guess: ")) #guesses
        if(guess==secret_number):
            print "You have won in %d guesses: "%num_guesses
            print ""
            return
        elif (guess<secret_number):
            print "Too low, try again"
        else:
            print "Too high, try again"
print "Guess the secret number between 1 and %d."%MAX_NUMBER
print ""
secret_number=choose_new_secret_number()
print "A new number has been chosen."
read_guesses()
command=raw_input( "Play again? (Y/N) ")
while(command=='y' or command=='Y'):
    secret_number=choose_new_secret_number()
    print "A new number has been chosen"
    read_guesses()
    command=raw_input( "Play again? (Y/N) ")          
    
Guess the secret number between 1 and 100.

A new number has been chosen.
Enter guess: 55
Too low, try again
Enter guess: 88
Too high, try again
Enter guess: 75
Too low, try again
Enter guess: 80
Too low, try again
Enter guess: 85
Too low, try again
Enter guess: 86
You have won in 6 guesses: 

Play again? (Y/N) y
A new number has been chosen
Enter guess: 50
Too low, try again
Enter guess: 70
Too low, try again
Enter guess: 90
Too low, try again
Enter guess: 95
Too low, try again
Enter guess: 99
Too high, try again
Enter guess: 97
Too low, try again
Enter guess: 98
You have won in 7 guesses: 

Play again? (Y/N) n

Example guess2.c, Page 226

In [3]:
import random
MAX_NUMBER=100
#def initialize_number_generator():
def new_secret_number():
    return (random.randint(1,MAX_NUMBER))#generate random number
def read_guesses(secret_number):
    num_guesses=0
    while(1): #guesses
        num_guesses=num_guesses+1
        guess=int(raw_input("Enter guess: "))
        if(guess==secret_number):
            print "You have won in %d guesses: "%num_guesses
            print ""
            return
        elif (guess<secret_number):
            print "Too low, try again"
        else:
            print "Too high, try again"
print "Guess the secret number between 1 and %d."%MAX_NUMBER
print ""
secret_number=new_secret_number()
print "A new number has been chosen."
read_guesses(secret_number)
command=raw_input( "Play again? (Y/N) ")
while(command=='y' or command=='Y'):
    secret_number=new_secret_number()
    print "A new number has been chosen"
    read_guesses(secret_number)
    command=raw_input( "Play again? (Y/N) ")          
    
Guess the secret number between 1 and 100.

A new number has been chosen.
Enter guess: 55
Too low, try again
Enter guess: 88
Too high, try again
Enter guess: 66
Too low, try again
Enter guess: 77
Too low, try again
Enter guess: 83
Too high, try again
Enter guess: 80
Too high, try again
Enter guess: 79
Too high, try again
Enter guess: 78
You have won in 8 guesses: 

Play again? (Y/N) n

Example poker.c, Page 233

In [ ]:
pairs=0

def read_cards():
    cards_read=0
    card_exists = [[False for i in xrange(NUM_SUITS)] for i in xrange(NUM_RANKS)]
    while(cards_read<NUM_CARDS):
        bad_card=False
        cardd=raw_input("Enter a card")
        a = cardd.split()
        rank_ch=a[0]
        suit_ch=a[1]
        if (rank_ch=='0'):
            exit(EXIT_SUCCESS)
        elif(rank_ch=='2'):
            rank=0
        elif(rank_ch=='3'):
            rank=1
        elif(rank_ch=='4'):
            rank=2
        elif(rank_ch=='5'):
            rank=3
        elif(rank_ch=='6'):
            rank=4
        elif(rank_ch=='7'):
            rank=5
        elif(rank_ch=='8'):
            rank=6
        elif(rank_ch=='9'):
            rank=7
        elif(rank_ch=='t' or rank_ch=='T'):
            rank=8
        elif(rank_ch=='j' or rank_ch=='J'):
            rank=9
        elif(rank_ch=='q' or rank_ch=='Q'):
            rank=10
        elif(rank_ch=='k' or rank_ch=='K'):
            rank=11
        elif(rank_ch=='a' or rank_ch=='A'):
            rank=12
        else:
            bad_card=True
        if(suit_ch=='c'or suit_ch=='C'):
            suit=0
        elif(suit_ch=='d' or suit_ch=='D'):
            suit=1
        elif(suit_ch=='h' or suit_ch=='H'):
            suit=1
        elif(suit_ch=='s' or suit_ch=='S'):
            suit=1
        else:
            bad_card=True
        if(bad_card):
            print "Bad card; ignored"
        elif (card_exists[rank][suit]):
            print "Duplicate card; ignored"
        else:
            num_in_rank[rank]=num_in_rank[rank]+1
            num_in_suit[suit]=num_in_suit[suit]+1
            card_exists[rank][suit]=True
            cards_read=cards_read+1

def analyse_hand():
    num_consec=0
    for suit in range(NUM_SUITS):
        if(num_in_suit[suit]==NUM_CARDS):
            flush=True
    rank=0
    while(num_in_rank[rank]==0):
        rank=rank+1
    while(rank<NUM_RANKS and num_in_rank[rank]>0):
        num_consec=num_consec+1
        rank=rank+1
    if(num_consec==NUM_CARDS):
        straight=True
        return
    for rank in range(NUM_RANKS):
        if(num_in_rank[rank]==4):
            four=True
        if(num_in_rank[rank]==3):
            three=True
        if(num_in_rank[rank]==2):
            pairs=pairs+1
            
def print_result():
    if(straight==True and flush==True):
        print "Straight flush"
    elif(four):
        print "Four of a kind"
    elif(three==True and pairs==1):
        print "Full house"
    elif(flush):
        print "Flush"
    elif(straight):
        print "Straight"
    elif(three):
        print "Three of a kind"
    elif(pairs==2):
        print "Two pairs"
    elif(pairs==1):
        print "Pair"
    else:
        print "High card"
    print ""
    
while(1):
    straight=False
    flush=False
    four=False
    three=False
    NUM_RANKS=13
    NUM_SUITS=4
    NUM_CARDS=5
    num_in_rank=[0]*NUM_RANKS
    num_in_suit=[0]*NUM_SUITS

    read_cards()
    analyse_hand()
    print_result()
        
Enter a card2 s
Enter a card5 s
Enter a card4 s
Enter a card3 s
Enter a card6 s
High card

In [ ]: