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]
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]
a = [ 22.2, 44.4, 66.6 ]
size = len(a)
for i in range(size):
print "\ta[" , i , "] = " , a[i]
a = [ 22.2, 44.4, 66.6 , 0 ,0,0,0]
size = len(a)
for i in range(size):
print "\ta[" , i , "] = " , a[i]
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]
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]
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
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
def sum_(a):
s = 0
for i in a:
s += i
return s
a = [ 11, 33, 55, 77 ]
print "sum(a) = " , sum_(a)
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 = []
read(a)
print "The array has " , len(a) , " elements: "
print_(a)
import sys
a = [ 22, 44, 66, 88 ]
print "a = " , id(a) # the address of a[0]
def index(x,a,n):
for i in range(len(a)):
if (a[i] == x):
return i
return n # x not found
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)
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)
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]
return n # x was not found in a[0..n-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)
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)
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]
return n # x was not found in a[0..n-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)
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]
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)
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 = []
read(a)
print_(a)
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: "
read(score)
print "The quiz averages are:"
printQuizAverages(score)
print "The class averages are: "
printClassAverages(score)
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"