Chapter 9: Arrays

Example 9.2, Page number: 9.2

In [1]:
letter='heavenly feeling'
letter=list(letter)

for count in letter:
    print count.upper(),
H E A V E N L Y   F E E L I N G

Example 9.8, Page number: 9.6

In [6]:
Sum=0.0
List=[]
n=5
for count in range(0,n):
    x=count+1
    print "\ni = %d   x = %d" %(count+1,x),
    List.append(count)
    Sum+=List[count]

avg=Sum/n
print "\n\nThe average is %5.2f\n\n" %avg
for count in range(0,n):
    d=List[count]-avg
    print "i=%d x=%5.2f d=%5.2f" %(count+1,List[count],d)
i = 1   x = 1 
i = 2   x = 2 
i = 3   x = 3 
i = 4   x = 4 
i = 5   x = 5 

The average is  2.00


i=1 x= 0.00 d=-2.00
i=2 x= 1.00 d=-1.00
i=3 x= 2.00 d= 0.00
i=4 x= 3.00 d= 1.00
i=5 x= 4.00 d= 2.00

Example 9.9, Page number: 9.8

In [7]:
n=5
List=[3.0,-2.0,12.0,4.4,3.5]
Sum=0.0

for count in range(0,n):
    Sum+=List[count]

avg=Sum/n

print "The average is %5.2f \n\n" %avg

for count in range(0,n):
    d=List[count]-avg
    print "i=%d  x=%5.2f  d=%5.2f\n" %(count+1,List[count],d)
The average is  4.18 


i=1  x= 3.00  d=-1.18

i=2  x=-2.00  d=-6.18

i=3  x=12.00  d= 7.82

i=4  x= 4.40  d= 0.22

i=5  x= 3.50  d=-0.68

Example 9.11, Page number: 9.10

In [8]:
def modify(a):
    print "From the function after modifying the values: "

    for count in range(0,3):
        a[count]=-9
        print "a[%d] = %d " %(count,a[count])

    return

a=[]
print "From main, before calling the function: "
for count in range(0,3):
    a.append(count+1)
    print "a[%d] = %d " %(count,a[count])

modify(a)
print "From the main, after calling the function: "
for count in range(0,3):
    print "a[%d] = %d " %(count,a[count])
From main, before calling the function: 
a[0] = 1 
a[1] = 2 
a[2] = 3 
From the function after modifying the values: 
a[0] = -9 
a[1] = -9 
a[2] = -9 
From the main, after calling the function: 
a[0] = -9 
a[1] = -9 
a[2] = -9 

Example 9.12, Page number: 9.12

In [9]:
a=1
def modify(b,c):

     print "From the function, after modifying the value : "
     global a
     a=-999
     b=-999
     print "a = %d  b = %d" %(a,b)
     for count in range(0,3):
         c[count]=-9
         print "c[%d] = %d" %(count,c[count])

     return



b=2
c=[]

print "From main, before calling the function: "
print "a = %d   b = %d" %(a,b)

for count in range(0,3):
    c.append(10*(count+1))
    print "c[%d] = %d" %(count,c[count])

modify(b,c)
print "From main, after calling the function:"
print "a = %d   b = %d" %(a,b)

for count in range(0,3):
    print "c[%d] = %d" %(count,c[count])
From main, before calling the function: 
a = 1   b = 2
c[0] = 10
c[1] = 20
c[2] = 30
From the function, after modifying the value : 
a = -999  b = -999
c[0] = -9
c[1] = -9
c[2] = -9
From main, after calling the function:
a = -999   b = 2
c[0] = -9
c[1] = -9
c[2] = -9

Example 9.13, Page number: 9.13

In [14]:
def reorder(n,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

x=[]
n=10
print

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

reorder(n,x)

print "\n\nReordered list of numbers: \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 

Reordered list of numbers: 

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 9.14, Page number: 9.16

In [24]:
def countwords(english):
    words=1
    for count in range(0,len(english)-1):
        if english[count]==' ' and english[count+1]!=' ':
            words+=1
            
    return words

def convert(words,english,piglatin):
    m1=0
    for n in range(1,words+1):
        count=m1
        while english[count]!=' ':
            m2=count
            count+=1

        for count in range(m1,m2):
            piglatin.append(english[count+1])
        piglatin.append(english[m1])
        piglatin.append('a')
        piglatin.append(' ')

        m1=m2+2

    return

def writeoutput(piglatin):
    piglatin=''.join(piglatin)
    print piglatin
    return

def main(english):
    english=list(english)
    piglatin=[]
    english.append(' ')

    words=countwords(english)
    convert(words,english,piglatin)
    writeoutput(piglatin)
    
    return

print '\nC is a popular structured programming language'
main('C is a popular structured programming language')
print '\nbaseball is the great American pastime.'
main('baseball is the great American pastime.')
print '\nthough there are many who prefer football'
main('though there are many who prefer football')
print '\nplease do not sneeza in the computer room'
main('please do not sneeza in the computer room')
C is a popular structured programming language
Ca sia aa opularpa tructuredsa rogrammingpa anguagela 

baseball is the great American pastime.
aseballba sia heta reatga mericanAa astime.pa 

though there are many who prefer football
houghta hereta reaa anyma howa referpa ootballfa 

please do not sneeza in the computer room
leasepa oda otna neezasa nia heta omputerca oomra 

Example 9.19, Page number: 9.26

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

    at=[]
    for row in range(0,m):
        temp=[]
        for col in range(0,n):
            t=i
            i+=1
            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    2    3    4    5
   6    7    8    9   10
  11   12   13   14   15
  16   17   18   19   20
  21   22   23   24   25

 SECOND TABLE : 

  50   51   52   53   54
  55   56   57   58   59
  60   61   62   63   64
  65   66   67   68   69
  70   71   72   73   74
Sums of the elements : 

  51   53   55   57   59
  61   63   65   67   69
  71   73   75   77   79
  81   83   85   87   89
  91   93   95   97   99