{ "metadata": { "name": "", "signature": "sha256:74348a5183e19ad7ac30f2bcfa88ff539f69c16e0607950b89f2f1031fc1e024" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 6 : Arrays" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.1, Page No 134" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iArray=range(5)\n", "\n", "for x in range(5):\n", " iArray[x]=0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.2, Page No 134" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iArray=range(5)\n", "\n", "for x in range(5):\n", " iArray[x]=x\n", " \n", "for x in range(5):\n", " print \"The value of iArray index \"+str(x)+\"is \"+str(x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of iArray index 0is 0\n", "The value of iArray index 1is 1\n", "The value of iArray index 2is 2\n", "The value of iArray index 3is 3\n", "The value of iArray index 4is 4\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.3, Page No 136" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iIndex = -1\n", "iArray=range(5)\n", "\n", "for x in range(5):\n", " iArray[x]=(x+5)\n", "\n", "while (iIndex < 0 or iIndex >4):\n", " iIndex=int(raw_input(\"Enter a valid index (0-4): \"))\n", " \n", "print \"The value of index \"+str(iIndex)+\" is \"+str(iArray[iIndex])" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a valid index (0-4): 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The value of index 4 is 9\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.4, Page No 137" ] }, { "cell_type": "code", "collapsed": false, "input": [ "cArray = range(5)\n", "cName=\"Olivia\"\n", "\n", "print \"\\nCharacter array not initialized:\\n\"\n", "\n", "for x in range(5):\n", " print \"Element \"+str(x)+\"'s Contents are \",cArray[x]\n", "\n", "print \"\\nInitialized character array: \"\n", "for x in range(6):\n", " print cName[x]," ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "Character array not initialized:\n", "\n", "Element 0's Contents are 0\n", "Element 1's Contents are 1\n", "Element 2's Contents are 2\n", "Element 3's Contents are 3\n", "Element 4's Contents are 4\n", "\n", "Initialized character array: \n", "O l i v i a\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.5, Page No 138" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iFound = -1\n", "iArray=range(5)\n", "\n", "for x in range(5):\n", " iArray[x]=(x+x)\n", " \n", "iValue=int(raw_input(\"Enter value to search for: \"))\n", "\n", "for x in range(5):\n", " if (iArray[x]==iValue):\n", " iFound=x\n", " break\n", "\n", "if(iFound > -1):\n", " print \"I found your search value in element \",iFound\n", "else:\n", " print \"Sorry, your search value was not found\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter value to search for: 8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "I found your search value in element 4\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.6, Page No 141" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iTwoD=[[0 for j in range(3)]for i in range(3)]\n", "for x in range(3):\n", " for y in range(3):\n", " iTwoD[x][y]=(x+y)\n", "for x in range(3):\n", " for y in range(3):\n", " print \"iTwoD[\"+str(x)+\"][\"+str(y)+\"]=\",iTwoD[x][y]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "iTwoD[0][0]= 0\n", "iTwoD[0][1]= 1\n", "iTwoD[0][2]= 2\n", "iTwoD[1][0]= 1\n", "iTwoD[1][1]= 2\n", "iTwoD[1][2]= 3\n", "iTwoD[2][0]= 2\n", "iTwoD[2][1]= 3\n", "iTwoD[2][2]= 4\n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.7, Page No 143" ] }, { "cell_type": "code", "collapsed": false, "input": [ "iTwoD=[[1,2,3],[4,5,6],[7,8,9]]\n", "iFoundAt=[0,0]\n", "\n", "iValue=0\n", "iFound=0\n", "\n", "iValue=int(raw_input(\"Enter your search value: \"))\n", "\n", "for x in range(3):\n", " for y in range(3):\n", " if(iTwoD[x][y]==iValue):\n", " iFound=1\n", " iFoundAt[0]=x\n", " iFoundAt[1]=y\n", " break\n", " \n", "if (iFound==1):\n", " print \"Found value in iTwoD [\"+str(iFoundAt[0])+\"][\"+str(iFoundAt[1])+\"]\"\n", "else:\n", " print \"Value not found\"" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter your search value: 7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Found value in iTwoD [2][0]\n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 6.8, Page No 145" ] }, { "cell_type": "code", "collapsed": false, "input": [ "board=range(9)\n", "cWhoWon=' '\n", "iCurrentPlayer=0\n", "\n", "def displayBoard():\n", " print \"\\n\\t|\\t|\\n\"\n", " print \"\\t|\\t|\\n\"\n", " print \"\"+str(board[0])+\"\\t|\"+str(board[1])+\"\\t|\"+str(board[2])+\"\\n\"\n", " print \"--------|-------|--------\\n\"\n", " print \"\"+str(board[3])+\"\\t|\"+str(board[4])+\"\\t|\"+str(board[5])+\"\\n\"\n", " print \"--------|-------|--------\\n\"\n", " print \"\\t|\\t|\\n\"\n", " print \"\"+str(board[6])+\"\\t|\"+str(board[7])+\"\\t|\"+str(board[8])+\"\\n\"\n", " print \"\\t|\\t|\\n\"\n", "\n", "def verifySelection(iSquare,iPlayer):\n", " if(board[iSquare-1]==' ' and (iPlayer==1 or iPlayer==0)):\n", " board[iSquare-1]='X'\n", " return 0\n", " elif(board[iSquare-1]==' ' and iPlayer==2):\n", " board[iSquare-1]='0'\n", " return 0\n", " else:\n", " return 1\n", " \n", "def checkForWin():\n", " global cWhoWon\n", " if(board[0]=='X' and board[1]=='X' and board[2]=='X'):\n", " cWhoWon='X'\n", " elif(board[3]=='X' and board[4]=='X' and board[5]=='X'):\n", " cWhoWon='X'\n", " elif(board[6]=='X' and board[7]=='X' and board[8]=='X'):\n", " cWhoWon='X'\n", " elif(board[0]=='X' and board[3]=='X' and board[6]=='X'):\n", " cWhoWon='X'\n", " elif(board[1]=='X' and board[4]=='X' and board[7]=='X'):\n", " cWhoWon='X'\n", " elif(board[2]=='X' and board[5]=='X' and board[8]=='X'):\n", " cWhoWon='X'\n", " elif(board[0]=='X' and board[5]=='X' and board[8]=='X'):\n", " cWhoWon='X'\n", " elif(board[2]=='X' and board[5]=='X' and board[6]=='X'):\n", " cWhoWon='X'\n", " elif(board[0]=='0' and board[1]=='0' and board[2]=='0'):\n", " cWhoWon='0'\n", " elif(board[3]=='0' and board[4]=='0' and board[5]=='0'):\n", " cWhoWon='0'\n", " elif(board[6]=='0' and board[7]=='0' and board[8]=='0'):\n", " cWhoWon='0'\n", " elif(board[0]=='0' and board[3]=='0' and board[6]=='0'):\n", " cWhoWon='0'\n", " elif(board[1]=='0' and board[4]=='0' and board[7]=='0'):\n", " cWhoWon='0'\n", " elif(board[2]=='0' and board[5]=='0' and board[8]=='0'):\n", " cWhoWon='0'\n", " elif(board[0]=='0' and board[5]=='0' and board[8]=='0'):\n", " cWhoWon='0'\n", " elif(board[2]=='0' and board[5]=='0' and board[6]=='0'):\n", " cWhoWon='0'\n", " \n", " if(cWhoWon!=' '):\n", " if(cWhoWon=='X'):\n", " print \"\\nX Wins!\\n\"\n", " return\n", " \n", " if(cWhoWon=='0'):\n", " print \"\\n0 Wins!\\n\"\n", " return\n", " else:\n", " return\n", " \n", " for x in range(9):\n", " if(board[x]!=' '):\n", " catTotal=catTotal+1\n", " \n", " if(catTotal==9):\n", " cWhoWon='C'\n", " print \"\\nCAT Game!\\n\"\n", " return\n", "iSquareNum=0\n", "\n", "for x in range(8):\n", " board[x]=' '\n", " \n", "displayBoard()\n", "\n", "while(cWhoWon==' '):\n", " print \"\\n\"+cWhoWon+\"\\n\"\n", " \n", " if(iCurrentPlayer==1 or iCurrentPlayer==0):\n", " print \"\\nPLAYER X\\n\"\n", " iSquareNum=int(raw_input(\"Enter an available square number (1-9): \"))\n", " \n", " if(verifySelection(iSquareNum,iCurrentPlayer)==1):\n", " iCurrentPlayer=1\n", " else:\n", " iCurrentPlayer=2\n", " else:\n", " print \"\\nPLAYER 0\\n\"\n", " iSquareNum=int(raw_input(\"Enter an available square number (1-9): \"))\n", " \n", " if(verifySelection(iSquareNum,iCurrentPlayer)==1):\n", " iCurrentPlayer=2\n", " else:\n", " iCurrentPlayer=1\n", " displayBoard()\n", " checkForWin()\n", " \n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t|\t|\n", "\n", "\t|\t|\n", "\n", " \t| \t| \n", "\n", "--------|-------|--------\n", "\n", " \t| \t| \n", "\n", "--------|-------|--------\n", "\n", "\t|\t|\n", "\n", " \t| \t|8\n", "\n", "\t|\t|\n", "\n", "\n", " \n", "\n", "\n", "PLAYER X\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an available square number (1-9): 1\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " \n", "\n", "\n", "PLAYER 0\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an available square number (1-9): 2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t|\t|\n", "\n", "\t|\t|\n", "\n", "X\t|0\t| \n", "\n", "--------|-------|--------\n", "\n", " \t| \t| \n", "\n", "--------|-------|--------\n", "\n", "\t|\t|\n", "\n", " \t| \t|8\n", "\n", "\t|\t|\n", "\n", "\n", " \n", "\n", "\n", "PLAYER X\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an available square number (1-9): 4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " \n", "\n", "\n", "PLAYER 0\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an available square number (1-9): 3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t|\t|\n", "\n", "\t|\t|\n", "\n", "X\t|0\t|0\n", "\n", "--------|-------|--------\n", "\n", "X\t| \t| \n", "\n", "--------|-------|--------\n", "\n", "\t|\t|\n", "\n", " \t| \t|8\n", "\n", "\t|\t|\n", "\n", "\n", " \n", "\n", "\n", "PLAYER X\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an available square number (1-9): 7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", " \n", "\n", "\n", "PLAYER 0\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter an available square number (1-9): 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t|\t|\n", "\n", "\t|\t|\n", "\n", "X\t|0\t|0\n", "\n", "--------|-------|--------\n", "\n", "X\t|0\t| \n", "\n", "--------|-------|--------\n", "\n", "\t|\t|\n", "\n", "X\t| \t|8\n", "\n", "\t|\t|\n", "\n", "\n", "X Wins!\n", "\n" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }