Chapter 10, Arrays¶

Program 10-1, Page Number : 227¶

In :
# Introduction to arrays

array = []		# defining array which is rather called list in python

print "You are to enter 5 integers.\n"

# Input the 5 integers into array "array"

for i in range(5) :  # equivalent to for i in range(0,5,1)
print "Enter integer #%d: "%(i+1),
val = int(raw_input())
array.insert(i,val)				# insert function is used to add elements to list :
#      insert(i,val) where i is the index and val is the value to be inserted

print "\nThank you.\n"

# Print out the integers stored in array "array"
j = 1	# used as a counter
for i in array:				# shorthand way of scanning through the list; using "in"
print "Integer #%d = %d."%(j,i)
j += 1
You are to enter 5 integers.

Enter integer #1:3
Enter integer #2:12
Enter integer #3:8
Enter integer #4:63
Enter integer #5:0

Thank you.

Integer #1 = 3.
Integer #2 = 12.
Integer #3 = 8.
Integer #4 = 63.
Integer #5 = 0.

Program 10-2, Page Number : 228¶

In :
# Initializing a global array

array = [ 4, 6, 5, 7, 2 ]

def main() :			# just a user-defined function ; not equivalent to the built-in main() function in C
""" prints the integers stored in array "array" """
j = 1		# counter
for i in array :
print "Integer #%d = %d."%(j,i)
j += 1

main()
Integer #1 = 4.
Integer #2 = 6.
Integer #3 = 5.
Integer #4 = 7.
Integer #5 = 2.

Program 10-3, Page Number: 230¶

In :
# The number of array elements actually used may vary

# Read in the size of the array
print "How many integers will you enter?",
array_size = 6
print array_size

array = [ 12, 3, -5, 21, 0, 899 ]	# Lists dynamically allocate memory
for i in range(array_size) :
print array[i],
# Print out the integers

print("\n")
for i in range(array_size) :
print "Integer #%d = %d."%(i+1,array[i])
How many integers will you enter? 6

12 3 -5 21 0 899

Integer #1 = 12.
Integer #2 = 3.
Integer #3 = -5.
Integer #4 = 21.
Integer #5 = 0.
Integer #6 = 899.

Program 10-4, Page Number: 231¶

In :
# The trailer method

# Macro definition
MAX_ARRAY_SIZE = 20
TRAILER = -9999

print "Please enter up to %d integers, using %d as a trailer:"%(MAX_ARRAY_SIZE,TRAILER)
array = [ 3, 75, -34, 6, -1, 2, 41, 0, 90, -9999]
for array_size in range(MAX_ARRAY_SIZE) :
print array[array_size],
if array[array_size] == -9999 :
break

# Print out the integers
print "\n\nYou have entered %d integers"%array_size
for i in range(array_size) :
print "Integer #%d = %d."%(i+1,array[i])
Please enter up to 20 integers, using -9999 as a trailer:
3 75 -34 6 -1 2 41 0 90 -9999

You have entered 9 integers
Integer #1 = 3.
Integer #2 = 75.
Integer #3 = -34.
Integer #4 = 6.
Integer #5 = -1.
Integer #6 = 2.
Integer #7 = 41.
Integer #8 = 0.
Integer #9 = 90.

Program 10-5, Page Number: 233¶

In :
# Caculating the average grade for a class

# Macro definition

MAX_GRADES = 20 	# The class may have up to 20 students
TRAILER = -9999		# -9999 marks the end of the grade list

grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]
break

# Find the average
sum = 0.0

41
92
15
65
0
78
81
96
-9999

Program 10-6, Page Number: 235¶

In :
# Determining which grades in a class are above the average

# Macro definition

MAX_GRADES = 20 	# The class may have up to 20 students
TRAILER = -9999		# -9999 marks the end of the grade list

grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]
break

# Find the average
sum = 0.0

# Find who got above average

print "\nThe following grades were above average:"
41
92
15
65
0
78
81
96
-9999

The following grades were above average:
92.000000 (student #2)
65.000000 (student #4)
78.000000 (student #6)
81.000000 (student #7)
96.000000 (student #8)

Program 10-7, Page Number 237¶

In :
# Reversing an array

# Macro definition
ARRAY_SIZE = 10 	# The array has 10 elements

array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]
for i in range(10) :
print array[i],

# Echo the array

print "\n\nThe original array was:"
for i in range(10) :
print array[i],

# Print the array in reverse

print "\n\nThe reversed array is:"
for i in range(ARRAY_SIZE-1,-1,-1) :
print array[i],
10 42 7 0 923 13 -5 6 19 3

The original array was:
10 42 7 0 923 13 -5 6 19 3

The reversed array is:
3 19 6 -5 13 923 0 7 42 10

Program 10-8, Page Number: 240¶

In :
import sys
# Reversing a character string

# Macro definition
MAX_STRING_SIZE = 10

print "Please enter a word of up to %d letters:"%MAX_STRING_SIZE
word = "bingo"
print word

# Echo the word

print "\nThe word is %s."%word

# Find thw word length
string_size = 0
for i in word :			# To iterate over the entire string
string_size += 1

# Print the word in reverse

print "\nThe reversed word is ",
for i in range(string_size-1,-1,-1) :
sys.stdout.write("%c"%word[i])  # For printing in the same line without implicit whitespaces

print
Please enter a word of up to 10 letters:
bingo

The word is bingo.

The reversed word is ognib

Program 10-9, Page Number: 242¶

In :
# Calculating the minimum and maximum grade in a class

# Macro definition

MAX_GRADES = 20 	# The class may have up to 20 students
TRAILER = -9999		# -9999 marks the end of the grade list

grades = [ 41, 92, 15, 65, 0, 78, 81, 96, -9999 ]
break

print "\nThe lowest grade is %f"%min

print "\nThe lowest grade is %f"%max
41
92
15
65
0
78
81
96
-9999

Program 10-10, Page Number: 243¶

In :
# Sorting an array

# Macro definition
MAX_ARRAY_SIZE = 20 	# The array may have up to 20 elements

array_size = 10
print "How many numbers?",
print array_size

while array_size < 1 or array_size > MAX_ARRAY_SIZE	:
print "How many numbers?",
print array_size

array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3]
for i in range(array_size) :
print array[i],

# Echo the array

print "\n\nThe original array is:"
for i in range(array_size) :
print array[i],

# Sort the array

for i in range(array_size-1) :
for j in range(i+1, array_size) :
if array[i] > array[j] :
temp = array[i]
array[i] = array[j]
array[j] = temp

# Printing the sorted array

print "\n\nThe sorted array is"
for i in range(array_size) :
print array[i],
How many numbers? 10
10 42 7 0 923 13 -5 6 19 3

The original array is:
10 42 7 0 923 13 -5 6 19 3

The sorted array is
-5 0 3 6 7 10 13 19 42 923

Program 10-11, Page Number: 247¶

In :
# Finding the median of an array

# Macro definition
MAX_ARRAY_SIZE = 20 	# The array may have up to 20 elements

array_size = 0
array = []

def get_size()	:
""" reads the size of the array (until a valid value is entered) """
global array_size
array_size = 10
print "How many numbers?",
print array_size
while array_size < 1 or array_size > MAX_ARRAY_SIZE	:
print "How many numbers?",
print array_size

""" read in the array """
global array
array = [ 10, 42, 7, 0, 923, 13, -5, 6, 19, 3 ]
for i in range(array_size) :
print array[i],

def write_array() :
""" write out the array """
for i in range(array_size) :
print array[i],

def sort_array() :
""" sort the array """
global array
for i in range(array_size-1) :
for j in range(i+1, array_size) :
if array[i] > array[j] :
temp = array[i]
array[i] = array[j]
array[j] = temp

def even(n) :
""" returns non-zero(true) if n is even else returns zero(false) """
return ((n/2)*2 == n)

get_size()		# Read in the size of the array

print "\n\nThe original array is:"
write_array()	# Echo the array

sort_array()	# Sort the array

print "\n\nThe sorted array is:"
write_array()	# Print the sorted array

print "\n\nThe median is:"
# The equivalent of tertiary conditional operator in python is a if test else b
print "%.1f"%( ((array[ array_size / 2 -1] + array[ array_size / 2 ]) / 2.0 ) if even(array_size) else float(array[ array_size / 2 ])  )
How many numbers? 10
10 42 7 0 923 13 -5 6 19 3

The original array is:
10 42 7 0 923 13 -5 6 19 3

The sorted array is:
-5 0 3 6 7 10 13 19 42 923

The median is:
8.5

Program 10-12, Page Number: 251¶

In :
# Tally how many of each number within a range are entered

# Macro definition
RANGE_LOW = 1
RANGE_HIGH = 10			# Tallies numbers between RANGE_LOW and RANGE_HIGH
TRAILER = -1 			# TRAILER marks end of data

inputfeed = [ 3, 5, 1, 10, 8, 5, 10, 6, 2, 11, 4, 9, -2, 3, 2, 9, -1 ]		# additional array for the sake of providing input

tally = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]		# empty array
# Input numbers
print "Please enter a number between %d and %d (%d to quit): "%(RANGE_LOW,RANGE_HIGH,TRAILER),
num = inputfeed
print num

i = 1
while num != TRAILER :
if num < RANGE_LOW or num > RANGE_HIGH : 		# Out of range
print "%d is not between %d and %d."%(num,RANGE_LOW,RANGE_HIGH)
else :
tally[ num - RANGE_LOW] += 1
print "\nEnter another number: ",
num = inputfeed[i]
print num
i += 1 			# to iterate over the input array

# Print totals

print "\n------------\n\nYou entered:"
for i in range(RANGE_HIGH - RANGE_LOW + 1) :
print "%2d %2d's"%(tally[i],i + RANGE_LOW)

print "\nThank you."
Please enter a number between 1 and 10 (-1 to quit):  3

Enter another number:  5

Enter another number:  1

Enter another number:  10

Enter another number:  8

Enter another number:  5

Enter another number:  10

Enter another number:  6

Enter another number:  2

Enter another number:  11
11 is not between 1 and 10.

Enter another number:  4

Enter another number:  9

Enter another number:  -2
-2 is not between 1 and 10.

Enter another number:  3

Enter another number:  2

Enter another number:  9

Enter another number:  -1

------------

You entered:
1  1's
2  2's
2  3's
1  4's
2  5's
1  6's
0  7's
1  8's
2  9's
2 10's

Thank you.

Program 10-13, Page Number: 255¶

In :
# Program to report car prices. User enters car make by number and car year.
# Program reports price. All data is fictitious

# 2-D array (4 X 12 ) of prices : row = make , column = year
price_list = [	[ 4775, 4980, 5222, 5305, 5483, 5547, 5596, 5713, 5842, 5903, 6043, 6230 ], # Volkswagen
[ 4853, 5140, 5413, 5590, 5723, 5848, 5762, 5944, 6104, 6255, 6370, 6526 ],	# Chevy
[ 5627, 5772, 5973, 6210, 6539, 6720, 6792, 6930, 7054, 7202, 7355, 7562 ], # Cadillac
[ 1576, 1738, 1970, 2151, 2205, 2280, 2442, 2580, 2814, 1953, 3078, 3201 ],	# Honda
]

def get_make() :
""" read in and return the number (make) of the desired car """

# Print the choice numbers and names
for i in range(1,5) :
print "%d="%i,
print_make(i)
print ",",
print "0=END"

# Read in the selection by number
print "     Which car do you want? ",
make = 2
print make
while make < 0 or make > 4 :
print "     Which car do you want? ",
make = 2
print make

return make

def get_year() :
""" read in and return the year of the desired car """

print "     What year (1975-1986)?",
year = 1980
print year
while year < 1975 and year > 1986 :
print "     What year (1975-1986)?",
year = 1980
print year

return year

def price (car,yr) :
""" Use "car" and "year" to generate an index into "price_list" and return the corresponding entry """
return price_list[ car - 1 ][ yr - 1975]

def print_make(car) :
""" print the make of "car" according to its value """
if car == 1 :
print "Volkswagen",
elif car == 2 :
print "Chevy",
elif car == 3 :
print " is $%d.\n"%price(make,year) make = 0 # To terminate the loop print "\nThank you for buying all those cars!" 1= Volkswagen , 2= Chevy , 3= Cadillac , 4= Honda , 0=END Which car do you want? 2 What year (1975-1986)? 1980 The price of a 1980 Chevy is$5848.