# Chapter 5: Arrays¶

## Program 5.1, page no. 186¶

In [1]:
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

Enter a grade: 58
Average of the ten grades entered is:  63


## Program 5.2, page no. 186¶

In [2]:
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 first five grades:
Enter the last five numbers in the same manner
Average of the ten grades entered is:  102


## Program 5.3, page no. 188¶

In [ ]:
grades = []
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 [6]:
grades = []
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

Enter the 10 grades:
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 [7]:
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 [11]:
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[0][i], size[1][i], ' ' if (size[1][i]==' ') else '/', size[2][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."

Enter the circumference of your head above your eyebrows in inches as a decimal value: 22.5
Your hat size is 7 1/4


## Program 5.7, page no. 206¶

In [12]:
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 [2]:
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[0][0], board[0][1], board[0][2])
print "---+---+---"
print " %c | %c | %c" %(board[1][0], board[1][1], board[1][2])
print "---+---+---"
print " %c | %c | %c" %(board[2][0], board[2][1], board[2][2])
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[0][0]==board[1][1] and board[0][0]==board[2][2]) or (board[0][2]==board[1][1] and board[0][2]==board[2][0])):
winner = player
else:
for line in range(0, 3):
if((board[line][0] == board[line][1] and board[line][0] == board[line][2]) or (board[0][line] == board[1][line] and board[0][line] == board[2][line])):
winner = player
i += 1

print "\n"
print " %c | %c | %c" %(board[0][0], board[0][1], board[0][2])
print "---+---+---"
print " %c | %c | %c" %(board[1][0], board[1][1], board[1][2])
print "---+---+---"
print " %c | %c | %c" %(board[2][0], board[2][1], board[2][2])

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!