# Chapter 6: Arrays¶

## Example 6.1, page no. 126¶

In [1]:
a = [0, 0, 0]
a[2] = 55.55
a[0] = 11.11
a[1] = 33.33
print "a[0] = " , a[0]
print "a[1] = " , a[1]
print "a[2] = " , a[2]

a[0] =  11.11
a[1] =  33.33
a[2] =  55.55


## Example 6.2, page no. 127¶

In [1]:
SIZE=5 # defines the size N for 5 elements
a = []
# declares the array's elements as type double
print "Enter " , SIZE , " numbers:\t"
for i in range(SIZE):
a.append(float(raw_input()))

print "In reverse order: "
for i in range(SIZE-1,-1,-1):
print "\t" , a[i]

Enter  5  numbers:
11.11
33.33
55.55
77.77
99.99
In reverse order:
99.99
77.77
55.55
33.33
11.11


## Example 6.3, page no. 128¶

In [3]:
a = [ 22.2, 44.4, 66.6 ]

size = len(a)
for i in range(size):
print "\ta[" , i , "] = " , a[i]

	a[ 0 ] =  22.2
a[ 1 ] =  44.4
a[ 2 ] =  66.6


## Example 6.4, page no. 128¶

In [4]:
a = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]
size = len(a)
for i in range(size):
print "\ta[" , i , "] = " , a[i]

	a[ 0 ] =  22.2
a[ 1 ] =  44.4
a[ 2 ] =  66.6
a[ 3 ] =  0
a[ 4 ] =  0
a[ 5 ] =  0
a[ 6 ] =  0


## Example 6.5, page no. 129¶

In [5]:
import numpy
SIZE = 4
a = numpy.zeros(4)
# declares the array's elements as type float
for i in range(SIZE):
print "\ta[" , i , "] = " , a[i]

	a[ 0 ] =  0.0
a[ 1 ] =  0.0
a[ 2 ] =  0.0
a[ 3 ] =  0.0


## Example 6.6, page no. 129¶

In [1]:
SIZE=4
a = [ 33.3, 44.4, 55.5, 66.6 ]
for i in range(7): # ERROR: index is out of bounds!
print "\ta[" , i , "] = " , a[i]

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-b39022f1d3ab> in <module>()
2 a = [ 33.3, 44.4, 55.5, 66.6 ]
3 for i in range(7): # ERROR: index is out of bounds!
----> 4     print "\ta[" , i , "] = " , a[i]
5

IndexError: list index out of range
	a[ 0 ] =  33.3
a[ 1 ] =  44.4
a[ 2 ] =  55.5
a[ 3 ] =  66.6
a[ 4 ] = 

## Example 6.7, page no. 130¶

In [7]:
a = [ 22.2, 44.4, 66.6 ]
x=11.1
print "x = " , x
a.append(88.8) # ERROR: index is out of bounds!
print "x = " , x

 x =  11.1
x =  11.1


## Example 6.8, page no. 130¶

In [8]:
a = [ 22.2, 44.4, 66.6 ]
x=11.1
print "x = " , x
a[3333] = 88.8 # ERROR: index is out of bounds!
print "x = " , x

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-8-63b6c8e60294> in <module>()
6 x=11.1
7 print "x = " , x
----> 8 a[3333] = 88.8 # ERROR: index is out of bounds!
9 print "x = " , x

IndexError: list assignment index out of range
x =  11.1


## Example 6.9, page no. 131¶

In [10]:
def sum_(a):
s = 0
for i in a:
s += i
return s

a = [ 11, 33, 55, 77 ]
print "sum(a) = " , sum_(a)

sum(a) =  176


## Example 6.10, page no. 132¶

In [12]:
def read(a):
print "Enter integers. Terminate with 0:\n"
n = 1
while True:
n = int(raw_input("a[" + str(len(a)) + "]: "))
if n == 0:
break
a.append(n)

def print_(a):
for i in a:
print i ,

a = []
print "The array has " , len(a) , " elements: "
print_(a)

Enter integers. Terminate with 0:

a[0]: 11
a[1]: 22
a[2]: 33
a[3]: 44
a[4]: 0
The array has  4  elements:
11 22 33 44


## Example 6.11, page no. 133¶

In [13]:
import sys
a = [ 22, 44, 66, 88 ]
print "a = " , id(a)  # the address of a[0]

a =  169156908


## Example 6.12, page no. 133¶

In [14]:
def index(x,a,n):
for i in range(len(a)):
if (a[i] == x):
return i

a = [ 22, 44, 66, 88, 44, 66, 55 ]
print "index(44,a,7) = " , index(44,a,7)
print "index(50,a,7) = " , index(50,a,7)

index(44,a,7) =  1
index(50,a,7) =  7


## Example 6.13, page no. 134¶

In [15]:
def sort(a,n):
# bubble sort:
n = len(a)
for i in range(n):
# bubble up max{a[0..n-i]}:
for j in range(n-i-1):
if (a[j] > a[j+1]):
a[j],a[j+1] = a[j+1],a[j]

def print_(a):
for i in range(len(a)):
print a[i],
print ''

a = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]

print_(a)
sort(a,8)
print_(a)

55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7
22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9


## Example 6.14, page no. 135¶

In [16]:
def index(x,a,n):
# PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];
# binary search:
lo=0
hi=n-1
while (lo <= hi):
i = (lo + hi)/2 # the average of lo and hi
if (a[i] == x):
return i
if (a[i] < x):
lo = i+1 # continue search in a[i+1..hi]
else:
hi = i-1 # continue search in a[lo..i-1]

a = [ 22, 33, 44, 55, 66, 77, 88 ]
print "index(44,a,7) = " , index(44,a,7)
print "index(60,a,7) = " , index(60,a,7)

index(44,a,7) =  2
index(60,a,7) =  7


## Example 6.15, page no. 136¶

In [17]:
def isNondecreasing(a,n):
# returns true iff a[0] <= a[1] <= ... <= a[n-1]:
for i in range(1,n):
if (a[i]<a[i-1]):
return False
return True

a = [ 22, 44, 66, 88, 44, 66, 55 ]
print "isNondecreasing(a,4) = " , isNondecreasing(a,4)
print "isNondecreasing(a,7) = " , isNondecreasing(a,7)

isNondecreasing(a,4) =  True
isNondecreasing(a,7) =  False


## Example 6.16, page no. 137¶

In [5]:
def isNondecreasing(a,n):
# returns true iff a[0] <= a[1] <= ... <= a[n-1]:
for i in range(1,n):
if (a[i]<a[i-1]):
return False
return True

def index(x,a,n):
# PRECONDITION: a[0] <= a[1] <= ... <= a[n-1];
# binary search:
assert(isNondecreasing(a,n))

lo=0
hi=n-1
while (lo <= hi):
i = (lo + hi)/2
if (a[i] == x):
return i
if (a[i] < x):
lo = i+1  # continue search in a[i+1..hi]
else:
hi = i-1  # continue search in a[lo..i-1]

a = [ 22, 33, 44, 55, 66, 77, 88, 60 ]
print "index(44,a,7) = " , index(44,a,7)
print "index(60,a,7) = " , index(60,a,7)

 index(44,a,7) =  2
index(60,a,8) =  7


## Example 6.17, page no. 137¶

In [19]:
Day =  [ 0, 1, 2, 3, 4, 5, 6 ]
high = [ 88.3, 95.0, 91.2, 89.9, 91.4, 92.5, 86.7]

for i in Day:
print "The high temperature for day " , i , " was " , high[i]

 The high temperature for day  0  was  88.3
The high temperature for day  1  was  95.0
The high temperature for day  2  was  91.2
The high temperature for day  3  was  89.9
The high temperature for day  4  was  91.4
The high temperature for day  5  was  92.5
The high temperature for day  6  was  86.7


## Example 6.18, page no. 139¶

In [20]:
def sort(a,n):
a.sort()

def print_(a,n):
for i in a:
print i,
print ''
a = [55.5, 22.5, 99.9, 66.6, 44.4, 88.8, 33.3, 77.7]
print_(a,8);
sort(a,8)
print_(a,8)

55.5 22.5 99.9 66.6 44.4 88.8 33.3 77.7
22.5 33.3 44.4 55.5 66.6 77.7 88.8 99.9


## Example 6.19, page no. 139¶

In [21]:
def read(a):
print "Enter 15 integers, 5 per row:\n"
for  i in range(3):
ar = []
print "Row " , i , ": ",
for j in range(5):
ar.append(int(raw_input()))
a.append(ar)

def print_(a):
for  i in range(3):
for j in range(5):
print a[i][j],
print ''
a = []
print_(a)

Enter 15 integers, 5 per row:

Row  0 : 44
77
33
11
44
Row  1 : 60
50
30
90
70
Row  2 : 85
25
45
45
55
44 77 33 11 44
60 50 30 90 70
85 25 45 45 55


## Example 6.20, page no. 140¶

In [24]:
def read(score):
for s in range(3):
print  "Student " , s , ": ",
st = []
for q in range(5):
st.append(int(raw_input()))
score.append(st)

def printQuizAverages(score):
for s in range(3):
sm = 0
for q in range(5):
sm += score[s][q]
print "\tStudent " ,  s , ": " , sm/5.0

def printClassAverages(score):
for q in range(5):
sm = 0
for s in range(3):
sm += score[s][q]
print "\tQuiz " , q , ": " , sm/3.0

NUM_STUDENTS = 3
NUM_QUIZZES = 5

score = []
print "Enter " , NUM_QUIZZES , " scores for each student: "
print "The quiz averages are:"
printQuizAverages(score)
print "The class averages are: "
printClassAverages(score)

Enter  5  scores for each student:
Student  0 : 8
7
9
8
9
Student  1 : 9
9
9
9
8
Student  2 : 5
6
7
8
9
The quiz averages are:
Student  0 :  8.2
Student  1 :  8.8
Student  2 :  7.0
The class averages are:
Quiz  0 :  7.33333333333
Quiz  1 :  7.33333333333
Quiz  2 :  8.33333333333
Quiz  3 :  8.33333333333
Quiz  4 :  8.66666666667


## Example 6.21, page no. 141¶

In [25]:
def numZeros(a,n1,n2,n3):
count = 0
for i in range(n1):
for j in range(n2):
for k in range(n3):
if (a[i][j][k] == 0):
count += 1
return count

a = [ [ [5,0,2], [0,0,9], [4,1,0], [7,7,7] ],[ [3,0,0], [8,5,0], [0,0,0], [2,0,9] ]]
print "This array has " , numZeros(a,2,4,3) , " zeros"

This array has  11  zeros