Chapter 6 : Arrays

Example 6.1, Page No 134

In [7]:
iArray=range(5)

for x in range(5):
    iArray[x]=0

Example 6.2, Page No 134

In [8]:
iArray=range(5)

for x in range(5):
    iArray[x]=x
    
for x in range(5):
    print "The value of iArray index "+str(x)+"is "+str(x)
The value of iArray index 0is 0
The value of iArray index 1is 1
The value of iArray index 2is 2
The value of iArray index 3is 3
The value of iArray index 4is 4

Example 6.3, Page No 136

In [11]:
iIndex = -1
iArray=range(5)

for x in range(5):
    iArray[x]=(x+5)

while (iIndex < 0 or iIndex >4):
    iIndex=int(raw_input("Enter a valid index (0-4): "))
    
print "The value of index "+str(iIndex)+" is "+str(iArray[iIndex])
Enter a valid index (0-4): 4
The value of index 4 is 9

Example 6.4, Page No 137

In [20]:
cArray = range(5)
cName="Olivia"

print "\nCharacter array not initialized:\n"

for x in range(5):
    print "Element "+str(x)+"'s Contents are ",cArray[x]

print "\nInitialized character array: "
for x in range(6):
    print cName[x],
Character array not initialized:

Element 0's Contents are  0
Element 1's Contents are  1
Element 2's Contents are  2
Element 3's Contents are  3
Element 4's Contents are  4

Initialized character array: 
O l i v i a

Example 6.5, Page No 138

In [7]:
iFound = -1
iArray=range(5)

for x in range(5):
    iArray[x]=(x+x)
    
iValue=int(raw_input("Enter value to search for: "))

for x in range(5):
    if (iArray[x]==iValue):
        iFound=x
        break

if(iFound > -1):
    print "I found your search value in element ",iFound
else:
    print "Sorry, your search value was not found"
Enter value to search for: 8
I found your search value in element  4

Example 6.6, Page No 141

In [13]:
iTwoD=[[0 for j in range(3)]for i in range(3)]
for x in range(3):
    for y in range(3):
        iTwoD[x][y]=(x+y)
for x in range(3):
    for y in range(3):
        print "iTwoD["+str(x)+"]["+str(y)+"]=",iTwoD[x][y]
iTwoD[0][0]= 0
iTwoD[0][1]= 1
iTwoD[0][2]= 2
iTwoD[1][0]= 1
iTwoD[1][1]= 2
iTwoD[1][2]= 3
iTwoD[2][0]= 2
iTwoD[2][1]= 3
iTwoD[2][2]= 4

Example 6.7, Page No 143

In [17]:
iTwoD=[[1,2,3],[4,5,6],[7,8,9]]
iFoundAt=[0,0]

iValue=0
iFound=0

iValue=int(raw_input("Enter your search value: "))

for x in range(3):
    for y in range(3):
        if(iTwoD[x][y]==iValue):
            iFound=1
            iFoundAt[0]=x
            iFoundAt[1]=y
            break
            
if (iFound==1):
    print "Found value in iTwoD ["+str(iFoundAt[0])+"]["+str(iFoundAt[1])+"]"
else:
    print "Value not found"
Enter your search value: 7
Found value in iTwoD [2][0]

Example 6.8, Page No 145

In [1]:
board=range(9)
cWhoWon=' '
iCurrentPlayer=0

def displayBoard():
    print "\n\t|\t|\n"
    print "\t|\t|\n"
    print ""+str(board[0])+"\t|"+str(board[1])+"\t|"+str(board[2])+"\n"
    print "--------|-------|--------\n"
    print ""+str(board[3])+"\t|"+str(board[4])+"\t|"+str(board[5])+"\n"
    print "--------|-------|--------\n"
    print "\t|\t|\n"
    print ""+str(board[6])+"\t|"+str(board[7])+"\t|"+str(board[8])+"\n"
    print "\t|\t|\n"

def verifySelection(iSquare,iPlayer):
    if(board[iSquare-1]==' ' and (iPlayer==1 or iPlayer==0)):
        board[iSquare-1]='X'
        return 0
    elif(board[iSquare-1]==' ' and iPlayer==2):
        board[iSquare-1]='0'
        return 0
    else:
        return 1
    
def checkForWin():
    global cWhoWon
    if(board[0]=='X' and board[1]=='X' and board[2]=='X'):
        cWhoWon='X'
    elif(board[3]=='X' and board[4]=='X' and board[5]=='X'):
        cWhoWon='X'
    elif(board[6]=='X' and board[7]=='X' and board[8]=='X'):
        cWhoWon='X'
    elif(board[0]=='X' and board[3]=='X' and board[6]=='X'):
        cWhoWon='X'
    elif(board[1]=='X' and board[4]=='X' and board[7]=='X'):
        cWhoWon='X'
    elif(board[2]=='X' and board[5]=='X' and board[8]=='X'):
        cWhoWon='X'
    elif(board[0]=='X' and board[5]=='X' and board[8]=='X'):
        cWhoWon='X'
    elif(board[2]=='X' and board[5]=='X' and board[6]=='X'):
        cWhoWon='X'
    elif(board[0]=='0' and board[1]=='0' and board[2]=='0'):
        cWhoWon='0'
    elif(board[3]=='0' and board[4]=='0' and board[5]=='0'):
        cWhoWon='0'
    elif(board[6]=='0' and board[7]=='0' and board[8]=='0'):
        cWhoWon='0'
    elif(board[0]=='0' and board[3]=='0' and board[6]=='0'):
        cWhoWon='0'
    elif(board[1]=='0' and board[4]=='0' and board[7]=='0'):
        cWhoWon='0'
    elif(board[2]=='0' and board[5]=='0' and board[8]=='0'):
        cWhoWon='0'
    elif(board[0]=='0' and board[5]=='0' and board[8]=='0'):
        cWhoWon='0'
    elif(board[2]=='0' and board[5]=='0' and board[6]=='0'):
        cWhoWon='0'
    
    if(cWhoWon!=' '):
        if(cWhoWon=='X'):
            print "\nX Wins!\n"
            return
        
        if(cWhoWon=='0'):
            print "\n0 Wins!\n"
            return
    else:
            return
    
    for x in range(9):
        if(board[x]!=' '):
            catTotal=catTotal+1
            
    if(catTotal==9):
        cWhoWon='C'
        print "\nCAT Game!\n"
        return
iSquareNum=0

for x in range(8):
    board[x]=' '
    
displayBoard()

while(cWhoWon==' '):
    print "\n"+cWhoWon+"\n"
    
    if(iCurrentPlayer==1 or iCurrentPlayer==0):
        print "\nPLAYER X\n"
        iSquareNum=int(raw_input("Enter an available square number (1-9): "))
        
        if(verifySelection(iSquareNum,iCurrentPlayer)==1):
            iCurrentPlayer=1
        else:
            iCurrentPlayer=2
    else:
        print "\nPLAYER 0\n"
        iSquareNum=int(raw_input("Enter an available square number (1-9): "))
        
        if(verifySelection(iSquareNum,iCurrentPlayer)==1):
            iCurrentPlayer=2
        else:
            iCurrentPlayer=1
        displayBoard()
        checkForWin()
        
        
	|	|

	|	|

 	| 	| 

--------|-------|--------

 	| 	| 

--------|-------|--------

	|	|

 	| 	|8

	|	|


 


PLAYER X

Enter an available square number (1-9): 1

 


PLAYER 0

Enter an available square number (1-9): 2

	|	|

	|	|

X	|0	| 

--------|-------|--------

 	| 	| 

--------|-------|--------

	|	|

 	| 	|8

	|	|


 


PLAYER X

Enter an available square number (1-9): 4

 


PLAYER 0

Enter an available square number (1-9): 3

	|	|

	|	|

X	|0	|0

--------|-------|--------

X	| 	| 

--------|-------|--------

	|	|

 	| 	|8

	|	|


 


PLAYER X

Enter an available square number (1-9): 7

 


PLAYER 0

Enter an available square number (1-9): 5

	|	|

	|	|

X	|0	|0

--------|-------|--------

X	|0	| 

--------|-------|--------

	|	|

X	| 	|8

	|	|


X Wins!