10 : ARRAYS

10.2.1,page number:170

In [1]:
import array
value = [6]

for index in range(0,6):
    print "Enter Integer"
    value.append(int(input()))
for index in range(0,7):
    print "Integer = ",value[index]
Enter Integer
4
Enter Integer
6
Enter Integer
12
Enter Integer
6
Enter Integer
7
Enter Integer
13
Integer =  6
Integer =  4
Integer =  6
Integer =  12
Integer =  6
Integer =  7
Integer =  13

10.3.1, page number:170

In [2]:
value=[6,4,3,2,1]
for index in xrange(5):
    print "Integer = ",value[index]
Integer =  6
Integer =  4
Integer =  3
Integer =  2
Integer =  1

10.3.2, page number:171

In [3]:
import array
def MAX_SIZE_CONST():
    return 123
def TRAILER_CONST():
    return -7777
value=array.array('i',[])
print "Enter upto",MAX_SIZE_CONST(),"using",TRAILER_CONST(),"as trailer :"
print ""
m=0
for index in range(0,int(MAX_SIZE_CONST())):
    item=int(input())
    if(item == int(TRAILER_CONST())):
        break
    else:
        value.append(item)
        m+=1
for index in range(0,m):
    print "Integer =",value[index]
Enter upto 123 using -7777 as trailer :

1
2
3
4
-7777
Integer = 1
Integer = 2
Integer = 3
Integer = 4

10.4.1, page number:172

In [4]:
import array
k=0
sum=0.0
def MAX_STUD():
    return 20
def ENDINP():
    return -9999
mark=array.array('i',(0 for i in range(0,MAX_STUD())))
print "enter upto",MAX_STUD(),"marks:"
readin=int(input())
for index in range(0,MAX_STUD()):
    if(readin != ENDINP()):
        mark[index]=readin
        readin=int(input())
        k+=1
for index in xrange(0,k):
    sum=sum+mark[index]
print "Average Mark = ",sum/k
    
enter upto 20 marks:
34
66
78
65
43
23
12
88
-9999
Average Mark =  51.125

10.5.1, page number:174

In [5]:
import array
def MAX_SIZE():
    return 10
#str=array.array('i',(0 for i in range(0,MAX_SIZE()+1)))
print "Enter the 10 letter word."
str=raw_input()
r=[]
for letter in str:
    r.append(letter)
r.reverse()
print "word = ",str
print "".join(r)
Enter the 10 letter word.
madurai
word =  madurai
iarudam

10.6.1, page number:175

In [6]:
import array
def MAX_SIZE():
    return 20
size=0
variable =array.array('i',[])
while True:
    print "Array-size : ?"
    size=int(input())
    if size>1 and size<MAX_SIZE():
        break
print "Enter Numbers:"
for k in range(0,size):
    m=int(input())
    variable.append(m)

variable= array.array('i',sorted(variable))

print "Sorted Array is:"
for k in range(0,size):
    print variable[k],
Array-size : ?
9
Enter Numbers:
7
10
43
0
-5
6
14
8
9
Sorted Array is:
-5 0 6 7 8 9 10 14 43

10.7.1,page number:176

In [1]:
pox =[[0 for x in range(4)]for x in range(6)]
pox[0]=3,5,7,9,12,20
pox[1]=10,13,17,21,25,40
pox[2]=15,18,22,26,30,45
pox[3]=17,20,23,27,32,46

def read_city():
    running=True
    while running:
        print "Which city?"
        city=int(input())
        if city == 0:
            exit(1)
        if city<4 or city>4 :
            running=False
    return city
def read_year():
    running=True
    while running:
        print "Which year?"
        year=int(input())
        if year>1950 or year<2000 :
            running=False
    return year
def pop(x,y):
    global pox
    return pox[x-1][(y-1950)/10]

def output_city():
    if city == 1:
        return "Madurai"
    elif city == 2:
        return "Madras"
    elif city == 3:
        return "Bombay"
    elif city == 4:
        return "Calcutta"
        
runnable=True
city=read_city()
while runnable :
    year=read_year()
    print "Population in ",year,"of",output_city(),"is",pop(city,year),"Lakhs"
    city =read_city()
    if city == 0:
        runnable=False
Which city?
3
Which year?
1990
Population in  1990 of Bombay is 30 Lakhs
Which city?
0