{ "metadata": { "name": "", "signature": "sha256:a29cdeb847e548d8c74cdb11f8f6f17380a9a763c13e45e4e17605b4ec6cf4a6" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 5: Arrays" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.1, page no. 186" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "count = 10\n", "sum = 0\n", "average = 0.0\n", "for i in range(0, count):\n", " print \"Enter a grade: \",\n", " grade = int(raw_input())\n", " sum += grade\n", " \n", "average = sum/count\n", "print \"Average of the ten grades entered is: \", average" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "58\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "68\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "79\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "55\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "90\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "24\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "68\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter a grade: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "47\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Average of the ten grades entered is: 63\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.2, page no. 186" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "sum = 0\n", "average = 0.0\n", "print \"Enter the first five grades: \"\n", "grade0 = int(raw_input(\"grade0: \"))\n", "grade1 = int(raw_input(\"grade1: \"))\n", "grade2 = int(raw_input(\"grade2: \"))\n", "grade3 = int(raw_input(\"grade3: \"))\n", "grade4 = int(raw_input(\"grade4: \"))\n", "\n", "print \"Enter the last five numbers in the same manner \"\n", "grade5 = int(raw_input(\"grade5: \"))\n", "grade6 = int(raw_input(\"grade6: \"))\n", "grade7 = int(raw_input(\"grade7: \"))\n", "grade8 = int(raw_input(\"grade8: \"))\n", "grade9 = int(raw_input(\"grade9: \"))\n", "\n", "sum = grade0 + grade1 + grade2 + grade3 + grade4 + grade5 + grade6 + grade7 + grade8 + grade9\n", "average = sum/10\n", " \n", "print \"Average of the ten grades entered is: \", average" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the first five grades: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade0: 58\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade1: 69\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade2: 48\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade3: 473\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade4: 37\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Enter the last five numbers in the same manner \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade5: 27\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade6: 95\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade7: 75\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade8: 74\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "grade9: 64\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Average of the ten grades entered is: 102\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.3, page no. 188" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "grades = []\n", "count = 10\n", "sum = 0\n", "average = 0.0\n", "\n", "print \"Enter the 10 grades: \"\n", "for i in range(0, count):\n", " print \"%2d> \" %(i + 1),\n", " grades.append(int(raw_input()))\n", " sum += grades[i]\n", "average = sum/count;\n", "print \"Average of the ten grades entered is: \", average" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.4, page no. 190" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "grades = []\n", "count = 10\n", "sum = 0\n", "average = 0.0\n", "\n", "print \"Enter the 10 grades: \"\n", "for i in range(0, count):\n", " print \"%2d> \" %(i + 1),\n", " grades.append(int(raw_input()))\n", " sum += grades[i]\n", "average = sum/count;\n", "for i in range(0, count):\n", " print \"Grade Number %2d is %3d\" %(i + 1, grades[i])\n", "print \"Average of the ten grades entered is: \", average" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the 10 grades: \n", " 1> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 2> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "87\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 3> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 4> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "90\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 5> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 6> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "54\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 7> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "43\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 8> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "45\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 9> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "67\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 10> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "89\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Grade Number 1 is 67\n", "Grade Number 2 is 87\n", "Grade Number 3 is 89\n", "Grade Number 4 is 90\n", "Grade Number 5 is 65\n", "Grade Number 6 is 54\n", "Grade Number 7 is 43\n", "Grade Number 8 is 45\n", "Grade Number 9 is 67\n", "Grade Number 10 is 89\n", "Average of the ten grades entered is: 69\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.5, page no. 192" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "import sys\n", "\n", "a = 1\n", "b = 2\n", "c = 3\n", "\n", "d = 4.0\n", "e = 5.0\n", "f = 6.0\n", "print \"A variable of type integer occupies %d bytes.\" %sys.getsizeof(int)\n", "print \"Here are the addresses of some variables of type integer:\"\n", "print \"The address of a is: %d The address of b is: %d\" %(id(a), id(b))\n", "print \"The address of c is: %d\" % id(c)\n", "print \"A variable of type float occupies %d bytes.\" %sys.getsizeof(float)\n", "print \"Here are the addresses of some variables of type float: \"\n", "print \"The address of d is: %d The address of e is: %d\" %(id(d), id(e))\n", "print \"The address of f is: \", id(f)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "A variable of type integer occupies 872 bytes.\n", "Here are the addresses of some variables of type integer:\n", "The address of a is: 21367976 The address of b is: 21367952\n", "The address of c is: 21367928\n", "A variable of type float occupies 872 bytes.\n", "Here are the addresses of some variables of type float: \n", "The address of d is: 32514136 The address of e is: 38248560\n", "The address of f is: 38248512\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.6, page no. 201" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "size = [['6', '6', '6', '6', '7', '7', '7', '7', '7', '7', '7', '7'],\n", " ['1', '5', '3', '7', ' ', '1', '1', '3', '1', '5', '3', '7'],\n", " ['2', '8', '4', '8', ' ', '8', '4', '8', '2', '8', '4', '8']]\n", "headsize = [164, 166, 169, 172, 175, 178, 181, 184, 188, 191, 194, 197]\n", "hat_found = False\n", "print \"Enter the circumference of your head above your eyebrows in inches as a decimal value: \",\n", "cranium = float(raw_input())\n", "your_head = int(8.0*cranium)\n", "i = 0\n", "if(your_head == headsize[i]):\n", " hat_found = True\n", "else:\n", " for i in range(1, len(headsize)):\n", " if(your_head > headsize[i - 1] and your_head <= headsize[i]):\n", " hat_found = True\n", " break\n", "if(hat_found):\n", " print \"Your hat size is %c %c%c%c\" %(size[0][i], size[1][i], ' ' if (size[1][i]==' ') else '/', size[2][i])\n", "else:\n", " if(your_head < headsize[0]):\n", " print \"You are the proverbial pinhead. No hat for you I'm afraid.\"\n", " else:\n", " print \"You, in technical parlance, are a fathead, No hat for you, I'm afraid.\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the circumference of your head above your eyebrows in inches as a decimal value: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "22.5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Your hat size is 7 1/4\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.7, page no. 206" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "print \"Enter the number of grades: \",\n", "nGrades = int(raw_input())\n", "grades = []\n", "sum = 0\n", " \n", "print \"Enter the %d grades: \" % nGrades\n", "\n", "for i in range(0, nGrades):\n", " print \"%d> \" %(i + 1),\n", " grades.append(int(raw_input()))\n", " sum += grades[i]\n", " \n", "print \"The grades you entered are: \"\n", "for i in range(0, nGrades):\n", " print \"Grade[%d] = %d \" %((i + 1), grades[i]),\n", " if((i+1) % 5 == 0):\n", " print \"\\n\"\n", " \n", "average = sum/nGrades\n", "print \"Average of the %d grades entered is: %.2f\" %(nGrades, average)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter the number of grades: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " Enter the 5 grades: \n", "1> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "56\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 2> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "78\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 3> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "98\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 4> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "65\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 5> " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "43\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The grades you entered are: \n", "Grade[1] = 56 Grade[2] = 78 Grade[3] = 98 Grade[4] = 65 Grade[5] = 43 \n", "\n", "Average of the 5 grades entered is: 68.00\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 5.8, page no. 213" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "player = 0\n", "winner = 0\n", "choice = 0\n", "row = 0\n", "column = 0\n", "board = [['1','2','3'],\n", " ['4','5','6'],\n", " ['7','8','9']]\n", "\n", "i = 0\n", "while(i < 9 and winner == 0):\n", " print \"\\n\"\n", " print \" %c | %c | %c\" %(board[0][0], board[0][1], board[0][2])\n", " print \"---+---+---\"\n", " print \" %c | %c | %c\" %(board[1][0], board[1][1], board[1][2])\n", " print \"---+---+---\"\n", " print \" %c | %c | %c\" %(board[2][0], board[2][1], board[2][2])\n", " player = i % 2 + 1\n", " while(choice < 0 or choice > 8 or board[row][column] > '9'):\n", " print \"Player %d, please enter a valid square number for where you want to place your %c: \" %(player,'X' if (player == 1) else 'O')\n", " choice = int(raw_input())\n", " choice = choice - 1\n", " row = choice/3\n", " column = choice % 3\n", " board[row][column] = 'X' if (player == 1) else 'O'\n", "\n", " 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])):\n", " winner = player\n", " else:\n", " for line in range(0, 3):\n", " 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])):\n", " winner = player\n", " i += 1\n", "\n", "print \"\\n\"\n", "print \" %c | %c | %c\" %(board[0][0], board[0][1], board[0][2])\n", "print \"---+---+---\"\n", "print \" %c | %c | %c\" %(board[1][0], board[1][1], board[1][2])\n", "print \"---+---+---\"\n", "print \" %c | %c | %c\" %(board[2][0], board[2][1], board[2][2])\n", " \n", "if(winner):\n", " print \"Congratulations, player %d, YOU ARE THE WINNER!\" % winner\n", "else:\n", " print \"How boring, it is a draw\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " 1 | 2 | 3\n", "---+---+---\n", " 4 | 5 | 6\n", "---+---+---\n", " 7 | 8 | 9\n", "\n", "\n", " X | 2 | 3\n", "---+---+---\n", " 4 | 5 | 6\n", "---+---+---\n", " 7 | 8 | 9\n", "Player 2, please enter a valid square number for where you want to place your O: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "2\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " X | O | 3\n", "---+---+---\n", " 4 | 5 | 6\n", "---+---+---\n", " 7 | 8 | 9\n", "Player 1, please enter a valid square number for where you want to place your X: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " X | O | 3\n", "---+---+---\n", " X | 5 | 6\n", "---+---+---\n", " 7 | 8 | 9\n", "Player 2, please enter a valid square number for where you want to place your O: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "7\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " X | O | 3\n", "---+---+---\n", " X | 5 | 6\n", "---+---+---\n", " O | 8 | 9\n", "Player 1, please enter a valid square number for where you want to place your X: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " X | O | 3\n", "---+---+---\n", " X | X | 6\n", "---+---+---\n", " O | 8 | 9\n", "Player 2, please enter a valid square number for where you want to place your O: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "8\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " X | O | 3\n", "---+---+---\n", " X | X | 6\n", "---+---+---\n", " O | O | 9\n", "Player 1, please enter a valid square number for where you want to place your X: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\n", " X | O | 3\n", "---+---+---\n", " X | X | 6\n", "---+---+---\n", " O | O | X\n", "Congratulations, player 1, YOU ARE THE WINNER!\n" ] } ], "prompt_number": 2 } ], "metadata": {} } ] }