Chapter 7: Functions

Example 7.1, Page number: 7.2

In [5]:
def lower_to_upper(c1):

    if c1>='a' and c1<='z':
        c2=chr(ord('A')+ord(c1)-ord('a'))

    else:
        c2=c1

    return c2

lower='g'
upper=lower_to_upper(lower)
print "the upper case equivalent is : ",upper
the upper case equivalent is :  G

Example 7.3, Page number: 7.5

In [6]:
def lower_to_upper(c1):

    if c1>='a' and c1<='z':
        return (chr(ord('A')+ord(c1)-ord('a')))
    else:
        return c1

lower='g'
upper=lower_to_upper(lower)
print "The uppercase equivalent is : ",upper
The uppercase equivalent is :  G

Example 7.4, Page number: 7.5

In [7]:
def maximum(x,y):
    if x>=y:
        z=x
    else:
        z=y

    print "Maximum value is : ",z

    return

maximum(5,6)
maximum(6,5)
maximum(5,5)
Maximum value is :  6
Maximum value is :  6
Maximum value is :  5

Example 7.5, Page number: 7.5

In [8]:
def factorial(n):
    prod=1
    if n>1:
        for i in range(2,n+1):
            prod=prod*i

    return prod

fact=factorial(6)
print "Its factorial is : ",fact
Its factorial is :  720

Example 7.7, Page number: 7.7

In [9]:
def maximum(x,y):

    if x>=y:
        z=x
    else:
        z=y

    print 'Maximum value is = ',z
    return

maximum(5,9)
maximum(8,3)
Maximum value is =  9
Maximum value is =  8

Example 7.9, Page number: 7.8

In [10]:
def maximum(x,y):
    if x>=y:
        z=x
    else:
        z=y

    return z

a=5
b=4
c=7

d=maximum(a,b)
print "Maximum = ",maximum(c,d)
Maximum =  7

Example 7.10, Page number: 7.10

In [11]:
def factorial(n):
    prod=1
    if n>1:
        for i in range(2,n+1):
            prod=prod*i

    return prod

n=7
fact=factorial(n)
print "Its factorial is : ",fact
Its factorial is :  5040

Example 7.11, Page number: 7.12

In [12]:
def play():
    print "Throwing the dice...."
    score1=throw()
    print "%2d" %(score1)

    if score1==7 or score1==11:
        print "Congratulations!! you WIN on the first throw"

    elif score1==2 or score1==3 or score1==12:
        print "sorry!! you LOSE on the first throw"

    else:
        while(True):
            print "Throwing the dice again..."
            score2=throw()
            print "%2d" %(score2)
            if score2==score1 or score2==7:
                break

        if score2==score1:
            print "You WIN by matching your first score"
        else:
            print "You LOSE by failing to match your first score"


    return


def throw():

    n1=random.randrange(1,7)
    n2=random.randrange(1,7)
    return n1+n2


import random


print "Welcome to the Game of Craps \n\n"
random.seed(2365)
play()
Welcome to the Game of Craps 


Throwing the dice....
 9
Throwing the dice again...
 4
Throwing the dice again...
11
Throwing the dice again...
 9
You WIN by matching your first score

Example 7.12, Page number: 7.18

In [13]:
def modify(a):
    a=a*3
    print "a= %d (from the function, after being modified)" %(a)
    return

a=2
print "a=%d (from main, before calling the function)" %(a)
modify(a)
print "a=%d (from main, after calling the function)" %(a)
a=2 (from main, before calling the function)
a= 6 (from the function, after being modified)
a=2 (from main, after calling the function)

Example 7.13, Page number: 7.19

In [14]:
def sl(val,n):
    deprec=val/n
    print "in ",n
    
    for year in range(1,n+1):
        val=val-deprec
        writeoutput(year,deprec,val)

    return

def ddb(val,n):
    for year in range(1,n+1):
        deprec=2*val/n
        val=val-deprec
        writeoutput(year,deprec,val)

    return

def syd(val,n):
    tag=val
    for year in range(1,n+1):
        deprec=(n-year+1)*tag/(n*(n+1)/2)
        val=val-deprec
        writeoutput(year,deprec,val)

    return

def writeoutput(year,depreciation,value):
    print "End of the year %2d  Depreciation: %7.2f  Current Value: %8.2f" %(year,depreciation,value)

    return



def main(choice,val,n):
    
    print "Original value : ",val
    val=float(val)
    print "Number of years : ",n

    if choice==1:
        print "Straight-Line Method\n\n"
        sl(val,n)
    elif choice==2:
        print "Double-Declining-Balance Method \n\n"
        ddb(val,n)
    elif choice==3:
        print "Sum-Of-The-Years'-Digits Method"
        syd(val,n)

    return


print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(1,8000,10)
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(2,8000,10)
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(3,8000,10)
print "\n\nMethod: (1-SL 2-DDB 3-SYD)"
main(1,5000,4)


    
    

Method: (1-SL 2-DDB 3-SYD)
Original value :  8000
Number of years :  10
Straight-Line Method


in  10
End of the year  1  Depreciation:  800.00  Current Value:  7200.00
End of the year  2  Depreciation:  800.00  Current Value:  6400.00
End of the year  3  Depreciation:  800.00  Current Value:  5600.00
End of the year  4  Depreciation:  800.00  Current Value:  4800.00
End of the year  5  Depreciation:  800.00  Current Value:  4000.00
End of the year  6  Depreciation:  800.00  Current Value:  3200.00
End of the year  7  Depreciation:  800.00  Current Value:  2400.00
End of the year  8  Depreciation:  800.00  Current Value:  1600.00
End of the year  9  Depreciation:  800.00  Current Value:   800.00
End of the year 10  Depreciation:  800.00  Current Value:     0.00


Method: (1-SL 2-DDB 3-SYD)
Original value :  8000
Number of years :  10
Double-Declining-Balance Method 


End of the year  1  Depreciation: 1600.00  Current Value:  6400.00
End of the year  2  Depreciation: 1280.00  Current Value:  5120.00
End of the year  3  Depreciation: 1024.00  Current Value:  4096.00
End of the year  4  Depreciation:  819.20  Current Value:  3276.80
End of the year  5  Depreciation:  655.36  Current Value:  2621.44
End of the year  6  Depreciation:  524.29  Current Value:  2097.15
End of the year  7  Depreciation:  419.43  Current Value:  1677.72
End of the year  8  Depreciation:  335.54  Current Value:  1342.18
End of the year  9  Depreciation:  268.44  Current Value:  1073.74
End of the year 10  Depreciation:  214.75  Current Value:   858.99


Method: (1-SL 2-DDB 3-SYD)
Original value :  8000
Number of years :  10
Sum-Of-The-Years'-Digits Method
End of the year  1  Depreciation: 1454.55  Current Value:  6545.45
End of the year  2  Depreciation: 1309.09  Current Value:  5236.36
End of the year  3  Depreciation: 1163.64  Current Value:  4072.73
End of the year  4  Depreciation: 1018.18  Current Value:  3054.55
End of the year  5  Depreciation:  872.73  Current Value:  2181.82
End of the year  6  Depreciation:  727.27  Current Value:  1454.55
End of the year  7  Depreciation:  581.82  Current Value:   872.73
End of the year  8  Depreciation:  436.36  Current Value:   436.36
End of the year  9  Depreciation:  290.91  Current Value:   145.45
End of the year 10  Depreciation:  145.45  Current Value:     0.00


Method: (1-SL 2-DDB 3-SYD)
Original value :  5000
Number of years :  4
Straight-Line Method


in  4
End of the year  1  Depreciation: 1250.00  Current Value:  3750.00
End of the year  2  Depreciation: 1250.00  Current Value:  2500.00
End of the year  3  Depreciation: 1250.00  Current Value:  1250.00
End of the year  4  Depreciation: 1250.00  Current Value:     0.00

Example 7.14, Page number: 7.24

In [15]:
def factorial(n):

    if n<=1:
        return 1

    else:
        return (n*factorial(n-1))


n=10
fact=factorial(n)
print "Its factorial is ",fact
Its factorial is  3628800

Example 7.15, Page number: 7.26

In [16]:
def reverse(text,n):

    if n<0:
        return
    else:
        print text[n],
        reverse(text,n-1)



text='Now is the time for all good men to come to tje aid of their country!'
n=len(text)
reverse(text,n-1)
    
! y r t n u o c   r i e h t   f o   d i a   e j t   o t   e m o c   o t   n e m   d o o g   l l a   r o f   e m i t   e h t   s i   w o N

Example 7.16, Page number: 7.16

In [17]:
def transfer(n,From,to,temp):

    if n>0:
        transfer(n-1,From,temp,to)
        print "Move disk %d from %c to %c" %(n,From,to)
        transfer(n-1,temp,to,From)

    return

print "Welcome to the TOWERS OF HANOI \n\n"
transfer(3,'L','R','C')
Welcome to the TOWERS OF HANOI 


Move disk 1 from L to R
Move disk 2 from L to C
Move disk 1 from R to C
Move disk 3 from L to R
Move disk 1 from C to L
Move disk 2 from C to R
Move disk 1 from L to R