Chapter 5: Arrays¶

Program 5.1, page no. 186¶

In :
count = 10
sum = 0
average = 0.0
for i in range(0, count):

average = sum/count
print "Average of the ten grades entered is: ", average
Average of the ten grades entered is:  63

Program 5.2, page no. 186¶

In :
sum = 0
average = 0.0
print "Enter the first five grades: "

print "Enter the last five numbers in the same manner "

average = sum/10

print "Average of the ten grades entered is: ", average
Enter the last five numbers in the same manner
Average of the ten grades entered is:  102

Program 5.3, page no. 188¶

In [ ]:
count = 10
sum = 0
average = 0.0

print "Enter the 10 grades: "
for i in range(0, count):
print "%2d> " %(i + 1),
average = sum/count;
print "Average of the ten grades entered is: ", average

Program 5.4, page no. 190¶

In :
count = 10
sum = 0
average = 0.0

print "Enter the 10 grades: "
for i in range(0, count):
print "%2d> " %(i + 1),
average = sum/count;
for i in range(0, count):
print "Average of the ten grades entered is: ", average
1> 67
2> 87
3> 89
4> 90
5> 65
6> 54
7> 43
8> 45
9> 67
10> 89
Average of the ten grades entered is:  69

Program 5.5, page no. 192¶

In :
import sys

a = 1
b = 2
c = 3

d = 4.0
e = 5.0
f = 6.0
print "A variable of type integer occupies %d bytes." %sys.getsizeof(int)
print "Here are the addresses of some variables of type integer:"
print "The address of a is: %d The address of b is: %d" %(id(a), id(b))
print "The address of c is: %d" % id(c)
print "A variable of type float occupies %d bytes." %sys.getsizeof(float)
print "Here are the addresses of some variables of type float: "
print "The address of d is: %d The address of e is: %d" %(id(d), id(e))
print "The address of f is: ", id(f)
A variable of type integer occupies 872 bytes.
Here are the addresses of some variables of type integer:
The address of a is: 21367976 The address of b is: 21367952
The address of c is: 21367928
A variable of type float occupies 872 bytes.
Here are the addresses of some variables of type float:
The address of d is: 32514136 The address of e is: 38248560
The address of f is:  38248512

Program 5.6, page no. 201¶

In :
size = [['6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7'],
['1', '5', '3', '7', ' ', '1', '1', '3', '1', '5', '3', '7'],
['2', '8', '4', '8', ' ', '8', '4', '8', '2', '8', '4', '8']]
headsize = [164, 166, 169, 172, 175, 178, 181, 184, 188, 191, 194, 197]
hat_found = False
print "Enter the circumference of your head above your eyebrows in inches as a decimal value: ",
cranium = float(raw_input())
i = 0
hat_found = True
else:
hat_found = True
break
if(hat_found):
print "Your hat size is %c %c%c%c" %(size[i], size[i], ' ' if (size[i]==' ') else '/', size[i])
else:
print "You are the proverbial pinhead. No hat for you I'm afraid."
else:
print "You, in technical parlance, are a fathead, No hat for you, I'm afraid."
Your hat size is 7 1/4

Program 5.7, page no. 206¶

In :
print "Enter the number of grades: ",
sum = 0

print "%d> " %(i + 1),

print "The grades you entered are: "
if((i+1) % 5 == 0):
print "\n"

Enter the number of grades: 5
1> 56
2> 78
3> 98
4> 65
5> 43

Average of the 5 grades entered is: 68.00

Program 5.8, page no. 213¶

In :
player = 0
winner = 0
choice = 0
row = 0
column = 0
board = [['1','2','3'],
['4','5','6'],
['7','8','9']]

i = 0
while(i < 9 and winner == 0):
print "\n"
print " %c | %c | %c" %(board, board, board)
print "---+---+---"
print " %c | %c | %c" %(board, board, board)
print "---+---+---"
print " %c | %c | %c" %(board, board, board)
player = i % 2 + 1
while(choice < 0 or choice > 8 or board[row][column] > '9'):
print "Player %d, please enter a valid square number for where you want to place your %c: " %(player,'X' if (player == 1) else 'O')
choice = int(raw_input())
choice = choice - 1
row = choice/3
column = choice % 3
board[row][column] = 'X' if (player == 1) else 'O'

if((board==board and board==board) or (board==board and board==board)):
winner = player
else:
for line in range(0, 3):
if((board[line] == board[line] and board[line] == board[line]) or (board[line] == board[line] and board[line] == board[line])):
winner = player
i += 1

print "\n"
print " %c | %c | %c" %(board, board, board)
print "---+---+---"
print " %c | %c | %c" %(board, board, board)
print "---+---+---"
print " %c | %c | %c" %(board, board, board)

if(winner):
print "Congratulations, player %d, YOU ARE THE WINNER!" % winner
else:
print "How boring, it is a draw"

1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9

X | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Player 2, please enter a valid square number for where you want to place your O:
2

X | O | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
Player 1, please enter a valid square number for where you want to place your X:
4

X | O | 3
---+---+---
X | 5 | 6
---+---+---
7 | 8 | 9
Player 2, please enter a valid square number for where you want to place your O:
7

X | O | 3
---+---+---
X | 5 | 6
---+---+---
O | 8 | 9
Player 1, please enter a valid square number for where you want to place your X:
5

X | O | 3
---+---+---
X | X | 6
---+---+---
O | 8 | 9
Player 2, please enter a valid square number for where you want to place your O:
8

X | O | 3
---+---+---
X | X | 6
---+---+---
O | O | 9
Player 1, please enter a valid square number for where you want to place your X:
9

X | O | 3
---+---+---
X | X | 6
---+---+---
O | O | X
Congratulations, player 1, YOU ARE THE WINNER!