Chapter 11: Pointers

Example 11.1, Page 11.2

In [11]:
u,v=[3],[]
pu=u
v=pu
pv=v

print "u=",u[0],
print "&u=",id(u),
print "pu=",id(u),
print "*pu",pu[0]

print "v=",v[0],
print "&v=",id(v),
print "pv=",id(v),
print "*pv",pv[0]
u= 3 &u= 56575840 pu= 56575840 *pu 3
v= 3 &v= 56575840 pv= 56575840 *pv 3

Example 11.2, Page number: 11.3

In [12]:
v=[3]
u1=2*(v[0]+5)
pv=v
u2=2*(pv[0]+5)

print "u1=%d u2=%d" %(u1,u2)
u1=16 u2=16

Example 11.3, Page munber: 11.4

In [13]:
v=[3]
pv=v
print "*pv=%d  v=%d" %(pv[0],v[0])

pv[0]=0
print "*pv=%d  v=%d" %(pv[0],v[0])
*pv=3  v=3
*pv=0  v=0

Example 11.7, Page number: 11.6

In [14]:
def funct1(u,v):
    u=0
    v=0
    print "Within funct1        :   u=%d    v=%d" %(u,v)
    return

def funct2(u,v):
    u[0]=0
    v[0]=0
    print "Within funct2        :  *pu=%d  *pv=%d" %(u[0],v[0])
    return

u=[1]
v=[3]

print "Before calling funct1:   u=%d    v=%d" %(u[0],v[0])
funct1(u[0],v[0])
print "After calling funct1 :   u=%d    v=%d" %(u[0],v[0])

print "Before calling funct2:   u=%d    v=%d" %(u[0],v[0])
funct2(u,v)
print "After calling funct2 :   u=%d    v=%d" %(u[0],v[0])
Before calling funct1:   u=1    v=3
Within funct1        :   u=0    v=0
After calling funct1 :   u=1    v=3
Before calling funct2:   u=1    v=3
Within funct2        :  *pu=0  *pv=0
After calling funct2 :   u=0    v=0

Example 11.8, Page number: 11.8

In [15]:
def scan_line(line,pv,pc,pd,pw,po):

    for c in line:

        if c=='A' or c=='E' or c=='I' or c=='O' or c=='U':
            pv[0]+=1
        elif c>='A' and c<='Z':
            pc[0]+=1
        elif c>='0' and c<='9':
            pd[0]+=1
        elif c==' ' or c=='\t':
            pw[0]+=1
        else:
            po[0]+=1

    return


vowel,consonants,digits,whitespc,other=[0],[0],[0],[0],[0]
line="Personal computers with memories in excess of 4096 KB are now quite common."
line=line.upper()
scan_line(line,vowel,consonants,digits,whitespc,other)
print "\n\n"
print "No. of vowels                : ",vowel[0]
print "No. of consonants            : ",consonants[0]
print "No. of digits                : ",digits[0]
print "No. of whitespace characters : ",whitespc[0]
print "No. of other characters      : ",other[0]


No. of vowels                :  23
No. of consonants            :  35
No. of digits                :  4
No. of whitespace characters :  12
No. of other characters      :  1

Example 11.12, Page number: 11.15

In [16]:
x=[10,11,12,13,14,15,16,17,18,19]

for i in range(0,10):
    print "i=%d   x[i]=%d   *(x+1)=%d" %(i,x[i],x[i]),
    print "   &x[i]=",id(x[i]),
    print "   x+i=",id(x[i])
i=0   x[i]=10   *(x+1)=10    &x[i]= 30696620    x+i= 30696620
i=1   x[i]=11   *(x+1)=11    &x[i]= 30696608    x+i= 30696608
i=2   x[i]=12   *(x+1)=12    &x[i]= 30696596    x+i= 30696596
i=3   x[i]=13   *(x+1)=13    &x[i]= 30696584    x+i= 30696584
i=4   x[i]=14   *(x+1)=14    &x[i]= 30696572    x+i= 30696572
i=5   x[i]=15   *(x+1)=15    &x[i]= 30696560    x+i= 30696560
i=6   x[i]=16   *(x+1)=16    &x[i]= 30696548    x+i= 30696548
i=7   x[i]=17   *(x+1)=17    &x[i]= 30696536    x+i= 30696536
i=8   x[i]=18   *(x+1)=18    &x[i]= 30696524    x+i= 30696524
i=9   x[i]=19   *(x+1)=19    &x[i]= 30696512    x+i= 30696512

Example 11.14, Page number: 11.17

In [17]:
def main():
    global x
    y="This string is declared within main"
    y=list(y)
    x=list(x)
    print x
    print y

    return

x="This string is declared externally"
main()
['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'e', 'x', 't', 'e', 'r', 'n', 'a', 'l', 'l', 'y']
['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ', 'd', 'e', 'c', 'l', 'a', 'r', 'e', 'd', ' ', 'w', 'i', 't', 'h', 'i', 'n', ' ', 'm', 'a', 'i', 'n']

Example 11.16, Page number: 11.19

In [18]:
def reorder(x):
    n=len(x)
    for item in range(0,n-1):
        for i in range(item+1,n):
            if x[i]<x[item]:
                temp=x[item]
                x[item]=x[i]
                x[i]=temp

    return

n=10
x=[]

for i in range(0,n):
    inp=i+1
    print "\ni=%d x=%d" %(i+1,inp),
    x.append(inp)

reorder(x)

print "\n"
for i in range(0,n):
    print "i=%d  x=%d" %(i+1,x[i])
i=1 x=1 
i=2 x=2 
i=3 x=3 
i=4 x=4 
i=5 x=5 
i=6 x=6 
i=7 x=7 
i=8 x=8 
i=9 x=9 
i=10 x=10 

i=1  x=1
i=2  x=2
i=3  x=3
i=4  x=4
i=5  x=5
i=6  x=6
i=7  x=7
i=8  x=8
i=9  x=9
i=10  x=10

Example 11.17, Page number: 11.21

In [19]:
i=[4]
i[0]=1
f=0.3
d=0.005
c='*'

px=i
print 'Values: i=%d f=%f d=%f c=%c \n' %(i[0],f,d,c)
print 'Addresses: &i=%X &f=%X &d=%X &c=%X \n' %(id(i[0]),id(f),id(d),id(c))
print 'Pointer Values: px=%X  px+1=%X  px+2=%X  px+3=%X' %(id(px[0]),id(px[0]+1),id(px[0]+2),id(px[0]+3))
Values: i=1 f=0.300000 d=0.005000 c=* 

Addresses: &i=1D46518 &f=32BD940 &d=32BD950 &c=1D9F158 

Pointer Values: px=1D46518  px+1=1D4650C  px+2=1D46500  px+3=1D464F4

Example 11.18, Page number: 11.22

In [20]:
a=[1,2,3,4,5,6]

px=a[0]
py=a[5]

print 'px=%X   py=%X \n' %(id(px),id(py))
print 'py - px = %X' %(id(py)-id(px))
px=1D46518   py=1D464DC 

py - px = -3C

Example 11.22, Page number: 11.26

In [21]:
def readinput(m,n,i=0):

    at=[]
    for row in range(0,m):
        temp=[]
        for col in range(0,n):
            t=i
            i+=2
            temp.append(t)
        at.append(temp)


    return at
        
def computesum(a,b,m,n):

    c=[]

    for row in range(0,m):
        temp=[]
        for col in range(0,n):
            t=a[row][col]+b[row][col]
            temp.append(t)
        c.append(temp)

    return c

def writeoutput(c,m,n):

    for row in range(0,m):
        for col in range(0,n):
            print "%4d" %(c[row][col]),
        print

    return



print "\n FIRST TABLE : \n"
a=readinput(5,5,1)
writeoutput(a,5,5)

print "\n SECOND TABLE : \n"
b=readinput(5,5,50)
writeoutput(b,5,5)

c=computesum(a,b,5,5)
print "Sums of the elements : \n"
writeoutput(c,5,5)
 FIRST TABLE : 

   1    3    5    7    9
  11   13   15   17   19
  21   23   25   27   29
  31   33   35   37   39
  41   43   45   47   49

 SECOND TABLE : 

  50   52   54   56   58
  60   62   64   66   68
  70   72   74   76   78
  80   82   84   86   88
  90   92   94   96   98
Sums of the elements : 

  51   55   59   63   67
  71   75   79   83   87
  91   95   99  103  107
 111  115  119  123  127
 131  135  139  143  147

Example 11.24, Page number: 11.31

In [22]:
def readinput(m,n,i=0):

    at=[]
    for row in range(0,m):
        temp=[]
        for col in range(0,n):
            t=i
            i+=2
            temp.append(t)
        at.append(temp)


    return at
        
def computesum(a,b,m,n):

    c=[]

    for row in range(0,m):
        temp=[]
        for col in range(0,n):
            t=a[row][col]+b[row][col]
            temp.append(t)
        c.append(temp)

    return c

def writeoutput(c,m,n):

    for row in range(0,m):
        for col in range(0,n):
            print "%4d" %(c[row][col]),
        print

    return



print "\n FIRST TABLE : \n"
a=readinput(5,5,1)
writeoutput(a,5,5)

print "\n SECOND TABLE : \n"
b=readinput(5,5,50)
writeoutput(b,5,5)

c=computesum(a,b,5,5)
print "Sums of the elements : \n"
writeoutput(c,5,5)
 FIRST TABLE : 

   1    3    5    7    9
  11   13   15   17   19
  21   23   25   27   29
  31   33   35   37   39
  41   43   45   47   49

 SECOND TABLE : 

  50   52   54   56   58
  60   62   64   66   68
  70   72   74   76   78
  80   82   84   86   88
  90   92   94   96   98
Sums of the elements : 

  51   55   59   63   67
  71   75   79   83   87
  91   95   99  103  107
 111  115  119  123  127
 131  135  139  143  147

Example 11.26, Page number: 11.34

In [23]:
def reorder(x):

    n=len(x)
    for item in range(0,n-1):
        for i in range(item+1,n):
            if x[item]>x[i]:
                temp=x[item]
                x[item]=x[i]
                x[i]=temp


    return

x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']
print 'Original list of strings :\n\n'

for i in x:
    print "String : ",i

reorder(x)

print "\nReodered list of strings : \n\n"

for i in x:
    print "String : ",i
Original list of strings :


String :  PACIFIC
String :  ATLANTIC
String :  INDIAN
String :  CARIBBEAN
String :  BERING
String :  BLACK
String :  RED
String :  NORTH
String :  BALTIC
String :  CASPIAN

Reodered list of strings : 


String :  ATLANTIC
String :  BALTIC
String :  BERING
String :  BLACK
String :  CARIBBEAN
String :  CASPIAN
String :  INDIAN
String :  NORTH
String :  PACIFIC
String :  RED

Example 11.28, Page number: 11.37

In [24]:
def convert(mm,dd,yy):
    yy-=1900
    ndays=long(30.42*(mm-1)+dd)
    if mm==2:
        ndays+=1
    if mm>2 and mm<8:
        ndays-=1
    if yy%4==0 and mm<2:
        ndays+=1

    ncycles=yy/4
    ndays+=ncycles*1461

    nyears=yy%4
    if nyears>0:
        ndays+=365*nyears+1
    if ndays>59:
        ndays-=1

    day=ndays%7

    return day

def main(mm,dd,yy):
    day_of_week=convert(mm,dd,yy)
    print "%s, %s %d %d" %(weekday[day_of_week],month[mm-1],dd,yy)
    return
    
weekday=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
month=['January','February','March','April','May','June','July','August',\
       'September','October','November','December']

main(10,29,1929)
print
main(8,15,1945)
print
main(7,20,1969)
print
main(5,24,1997)
print
main(8,30,2010)
print
main(4,12,2069)
Tuesday, October 29 1929

Wednesday, August 15 1945

Sunday, July 20 1969

Saturday, May 24 1997

Monday, August 30 2010

Friday, April 12 2069

Example 11.30, Page number: 11.44

In [25]:
def ratio_choose(freq,a,m,n):

    if freq=='C':
        ratio=md3(a,m,n)
    elif freq=='D':
        ratio=md2(a,m,n)
    else:
        ratio=md1(a,m,n)

    return ratio


def table(freq,a,m,n):


    print "Interest Rate          Future Amount\n\n"
    for count in range(1,21):
        i=0.01*count
        f=a*ratio_choose(freq,i,m,n)
        print "    %2d                      %.2f" %(count,f)

    return

def md1(i,m,n):
    
    factor=1+i/m
    ratio=12*(factor**(m*n)-1)/i
    return ratio

def md2(i,m,n):

    factor=1+i/m
    ratio=(factor**(m*n)-1)/(factor**(m/12)-1)
    return ratio

def md3(i,dummy,n):
    ratio=(10**(i*n)-1)/(10**(i/12)-1)
    return ratio

def main(freq):
    m=0
    freq=freq.upper()
    if freq=='A':
        m=1
        print "\nAnual Compounding\n"
        
    elif freq=='S':
        m=2
        print "\nSemiannual Compounding\n"
        
    elif freq=='Q':
        print "\nQuaterly Compounding\n"
        m=4
        
    elif freq=='M':
        m=12
        print "\nMonthly Compounding\n"
        
    elif freq=='D':
        m=360
        print "\nDaily Compounding\n"

    elif freq=='C':
        m=0
        print "\nContinuous Compounding\n"
        
    else:
        print "\nERROR!!! Please Repeat\n\n"
        return
        
    return m


a,n,freq=100,3,'m'
print "FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS\n\n"
print "Frequency of Compunding (A,S,Q,M,D,C): ",freq
print "Amount of Each Monthly Payement : ",a
print "Number of years: ",n
m=main(freq)

table(freq,a,m,n)
FUTURE VALUE OF A SERIES OF MONTHLY DEPOSITS


Frequency of Compunding (A,S,Q,M,D,C):  m
Amount of Each Monthly Payement :  100
Number of years:  3

Monthly Compounding

Interest Rate          Future Amount


     1                      3653.00
     2                      3707.01
     3                      3762.06
     4                      3818.16
     5                      3875.33
     6                      3933.61
     7                      3993.01
     8                      4053.56
     9                      4115.27
    10                      4178.18
    11                      4242.31
    12                      4307.69
    13                      4374.33
    14                      4442.28
    15                      4511.55
    16                      4582.17
    17                      4654.18
    18                      4727.60
    19                      4802.45
    20                      4878.78
In [25]: