{ "metadata": { "name": "", "signature": "sha256:6ada72776bf91dd75540d29fecfeda6a5cab8bde3c866cd455973111367e4933" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Chapter 9: More on Functions" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.1, page no. 351" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "def sum(x, y):\n", " return x + y\n", " \n", "def product(x, y):\n", " return x * y\n", " \n", "def difference(x, y):\n", " return x - y\n", "\n", "a = 10\n", "b = 5\n", "pfun = sum # points to sum() \n", "result = pfun(a, b)\n", "print \"pfun = sum result = \", result\n", "pfun = product # points to product()\n", "result = pfun(a, b)\n", "print \"pfun = product result = \", result\n", "pfun = difference # points to difference()\n", "result = pfun(a, b)\n", "print \"pfun = difference result = \", result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "pfun = sum result = 15\n", "pfun = product result = 50\n", "pfun = difference result = 5\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.2, page no. 353" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def sum(x, y):\n", " return x + y\n", " \n", "def product(x, y):\n", " return x * y\n", " \n", "def difference(x, y):\n", " return x - y\n", "\n", "a = 10\n", "b = 5\n", "result = 0\n", "pfun = []\n", "pfun.append(sum)\n", "pfun.append(product)\n", "pfun.append(difference)\n", "\n", "for i in range(3):\n", " result = pfun[i](a, b)\n", " print \"result = \", result\n", "\n", "result = pfun[1](pfun[0](a, b), pfun[2](a, b))\n", "print \"The product of the sum and the difference = \", result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "result = 15\n", "result = 50\n", "result = 5\n", "The product of the sum and the difference = 75\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.3, page no. 356" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def sum(x, y):\n", " return x + y\n", " \n", "def product(x, y):\n", " return x * y\n", " \n", "def difference(x, y):\n", " return x - y\n", " \n", "def any_function(pfun , x, y):\n", " return pfun(x, y);\n", "\n", "a = 10\n", "b = 5\n", "pf = sum\n", "result = any_function(pf, a, b)\n", "print \"result = \", result\n", "result = any_function(product, a, b)\n", "print \"result = \", result\n", "print \"result = \", any_function(difference, a, b)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "result = 15\n", "result = 50\n", "result = 5\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.4, page no. 359" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def test1():\n", " count = 0\n", " count += 1\n", " print \"test1 count = \", count\n", "\n", "def test2():\n", " count = 0\n", " count += 1\n", " print \"test2 count = \", count\n", "\n", "for i in range(5):\n", " test1()\n", " test2()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "test1 count = 1\n", "test2 count = 1\n", "test1 count = 1\n", "test2 count = 1\n", "test1 count = 1\n", "test2 count = 1\n", "test1 count = 1\n", "test2 count = 1\n", "test1 count = 1\n", "test2 count = 1\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.5, page no. 361" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "count = 0\n", "\n", "def test1():\n", " global count\n", " count += 1\n", " print \"test1 count = \", count\n", "\n", "def test2():\n", " count = 0\n", " count += 1\n", " print \"test2 count = \", count\n", "\n", "for i in range(5):\n", " test1()\n", " test2()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "test1 count = 1\n", "test2 count = 1\n", "test1 count = 2\n", "test2 count = 1\n", "test1 count = 3\n", "test2 count = 1\n", "test1 count = 4\n", "test2 count = 1\n", "test1 count = 5\n", "test2 count = 1\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.6, page no. 364" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def factorial(n):\n", " if(n < 2):\n", " return n\n", " return n*factorial(n - 1)\n", " \n", "print \"Enter an integer value: \",\n", "number = int(raw_input())\n", "print \"The factorial of %d is %d \"% (number, factorial(number))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter an integer value: " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The factorial of 5 is 120 \n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.7, page no. 368" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "def average(*arg):\n", " sum = arg[0] + arg[1]\n", " count = 2\n", " for i in arg:\n", " count += 1\n", " sum += i\n", " average = sum/count\n", " return average\n", " \n", "v1 = 10.5\n", "v2 = 2.5\n", "num1 = 6\n", "num2 = 5\n", "num3 = 12\n", "num4 = 20\n", "print \"Average = %.2f \" %(average(v1, 3.5, v2, 4.5, 0.0))\n", "print \"Average = %.2f \" %(average(1.0, 2.0, 0.0))\n", "print \"Average = %.2f \" %(average(num2, v2, num1, num4, num3, 0.0))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Average = 5.00 \n", "Average = 1.20 \n", "Average = 6.62 \n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.8, page no. 370" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "import sys\n", "\n", "print \"Program name: \", sys.argv[0]\n", "for i in range(1, len(sys.argv)):\n", " print \"Argument %d: %s \"%(i, sys.argv[i])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Program name: -c\n", "Argument 1: -f \n", "Argument 2: /tmp/tmpL28uUI/profile_default/security/kernel-d2752270-d981-4bb2-b2bc-3d505dfa3dd5.json \n", "Argument 3: --IPKernelApp.parent_appname='ipython-notebook' \n", "Argument 4: --profile-dir \n", "Argument 5: /tmp/tmpL28uUI/profile_default \n", "Argument 6: --parent=1 \n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Program 9.9, page no. 377" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "SIZE = 6\n", "comp_c = '@'\n", "player_c = '0'\n", "board = [[0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0],\n", " [0, 0, 0, 0, 0, 0]]\n", "\n", "moves = [[False, False, False, False, False, False],\n", " [False, False, False, False, False, False],\n", " [False, False, False, False, False, False],\n", " [False, False, False, False, False, False],\n", " [False, False, False, False, False, False],\n", " [False, False, False, False, False, False]]\n", "no_of_moves = 0\n", "invalid_moves = 0\n", "again = 0\n", "next_player = True\n", "\n", "def get_score(board, player):\n", " global comp_c\n", " global player_c\n", " return player_counters(board, player) - player_counters(board, (comp_c if player == player_c else player_c)) \n", " \n", "def player_counters(board,player):\n", " count = 0;\n", " for row in range(SIZE):\n", " for col in range(SIZE):\n", " if(board[row][col] == player):\n", " count += 1\n", " return count;\n", "\n", "def computer_move(board,moves, player):\n", " best_row = 0;\n", " best_col = 0;\n", " new_score = 0;\n", " score = SIZE*SIZE;\n", " temp_board = []\n", " temp_moves = []\n", " for i in range(SIZE):\n", " a = []\n", " b = []\n", " for j in range(SIZE):\n", " a.append(0)\n", " b.append(False)\n", " temp_board.append(a)\n", " temp_moves.append(b) \n", " opponent = comp_c if (player == player_c) else player_c\n", " for row in range(SIZE):\n", " for col in range(SIZE):\n", " if(not moves[row][col]):\n", " continue;\n", " temp_board = board\n", " make_move(temp_board, row, col, player);\n", " valid_moves(temp_board, temp_moves, opponent);\n", " new_score = best_move(temp_board, temp_moves, opponent);\n", " if(new_score < score):\n", " score = new_score;\n", " best_row = row;\n", " best_col = col;\n", " make_move(board, best_row, best_col, player);\n", "\n", " \n", "def best_move(board, moves, player):\n", " new_board = []\n", " for i in range(SIZE):\n", " a = []\n", " for j in range(SIZE):\n", " a.append(0)\n", " new_board.append(a)\n", " score = 0;\n", " new_score = 0;\n", "\n", " for row in range(SIZE):\n", " for col in range(SIZE):\n", " if(not moves[row][col]):\n", " continue;\n", " new_board = board\n", " make_move(new_board, row, col, player);\n", " new_score = get_score(new_board, player);\n", " if(score < new_score):\n", " score = new_score;\n", " return score;\n", "\n", "\n", "def make_move(board,row,col,player):\n", " rowdelta = 0;\n", " coldelta = 0;\n", " x = 0;\n", " y = 0;\n", " if player == player_c:\n", " opponent = comp_c\n", " else:\n", " opponent = player_c \n", " \n", " board[row][col] = player\n", " for rowdelta in range(-1,2):\n", " for coldelta in range(-1,2):\n", " if((row == 0 and rowdelta == -1) or row + rowdelta >= SIZE or(col == 0 and coldelta == -1) or col + coldelta >= SIZE or\n", "(rowdelta == 0 and coldelta == 0)):\n", " continue;\n", "\n", " if(board[row + rowdelta][col + coldelta] == opponent):\n", " x = row + rowdelta;\n", " y = col + coldelta;\n", " \n", " while True:\n", " x += rowdelta;\n", " y += coldelta;\n", " if(x >= SIZE or y >= SIZE or board[x][y] == ' '):\n", " break;\n", " if(board[x][y] == player):\n", " x -= rowdelta\n", " y -= coldelta\n", " while(board[x][y] == opponent):\n", " board[x][y] = player;\n", " x -= rowdelta\n", " y -= coldelta\n", " \n", " break;\n", "\n", "def reset_board(board):\n", " global SIZE\n", " global player_c\n", " global comp_c\n", " for row in range(SIZE):\n", " for col in range(SIZE):\n", " board[row][col] = ' '\n", " \n", " mid = SIZE/2\n", " board[mid][mid] = player_c\n", " board[mid-1][mid-1] = board[mid][mid]\n", " board[mid][mid - 1] = comp_c\n", " board[mid-1][mid] = board[mid][mid - 1]\n", "\n", "def display(board):\n", " col_label = 'a'\n", " print \"\"\n", " for col in range(SIZE):\n", " print \"%c\" %((chr(ord(col_label) + col))),\n", " print \"\"\n", " for row in range(SIZE):\n", " print \" +\",\n", " for col in range(SIZE):\n", " print \"---+\",\n", " print \"\\n%2d|\" %(row + 1)\n", " \n", " for col in range(SIZE):\n", " print \" %c |\" %(board[row][col]),\n", " print \"\"\n", " print \" +\"\n", " for col in range(SIZE):\n", " print \"---+\",\n", " print \"\"\n", "\n", "def valid_moves(board, moves, player):\n", " global SIZE\n", " global player_c\n", " global comp_c\n", " rowdelta = 0\n", " coldelta = 0\n", " x = 0\n", " y = 0\n", " global no_of_moves\n", " opponent = comp_c if (player == player_c) else player_c\n", " for row in range(SIZE):\n", " for col in range(SIZE):\n", " moves[row][col] = False\n", " for row in range(SIZE):\n", " for col in range(SIZE):\n", " if(board[row][col] != ' '):\n", " continue\n", " for rowdelta in range(-1, rowdelta+1):\n", " for coldelta in range(-1, coldelta+1):\n", " if((row == 0 and rowdelta == -1) or row + rowdelta >= SIZE or (col == 0 and coldelta == -1) or col + coldelta >= SIZE or (rowdelta == 0 and coldelta == 0)):\n", " continue\n", " if(board[row + rowdelta][col + coldelta] == opponent):\n", " x = row + rowdelta\n", " y = col + coldelta \n", " while(True):\n", " x += rowdelta\n", " y += coldelta\n", " if(x < 0 or x >= SIZE or y < 0 or y >= SIZE or board[x][y] == ' '):\n", " break\n", " if(board[x][y] == player):\n", " moves[row][col] = True\n", " no_of_moves += 1\n", " break\n", " return no_of_moves\n", "\n", " \n", "print \"REVERSI\"\n", "print \"You can go first on the first game, then we will take turns.\"\n", "print \"You will be white - (%c)\\nI will be black - (%c). \" %(player_c, comp_c)\n", "print \"Select a square for your move by typing a digit for the row and a letter for the column with no spaces between.\"\n", "print \"Good luck! Press Enter to start.\"\n", "raw_input()\n", "while(True):\n", " reset_board(board)\n", " next_player = not(next_player)\n", " no_of_moves = 4\n", " while(True):\n", " display(board)\n", " next_player = not next_player\n", " if(True == next_player):\n", " if(valid_moves(board, moves, player_c)):\n", " while(True):\n", " print \"Please enter your move (row column - no space): \",\n", " x = int(raw_input(\"row: \"))\n", " y = raw_input(\"col: \")\n", " y = ord(chr(ord(y.lower()) - ord('a')))\n", " x -= 1\n", " if(y < 0 or y >= SIZE or x >= SIZE or (not moves[x][y])):\n", " print \"Not a valid move, try again.\\n\"\n", " continue\n", " make_move(board, x, y, player_c)\n", " no_of_moves += 1\n", " break\n", " else:\n", " invalid_moves += 1\n", " if(invalid_moves < 2):\n", " print \"You have to pass, press return\",\n", " again = raw_input()\n", " else:\n", " print \"\\nNeither of us can go, so the game is over. \"\n", " else:\n", " if(valid_moves(board, moves, comp_c)):\n", " invalid_moves = 0\n", " computer_move(board, moves, comp_c)\n", " no_of_moves += 1\n", " else:\n", " invalid_moves += 1\n", " if(invalid_moves < 2):\n", " print \"I have to pass, your go \"\n", " else:\n", " print \"Neither of us can go, so the game is over.\",\n", " display(board)\n", " print \"The final score is: \"\n", " print \"Computer %d User %d \" %(player_counters(board, comp_c), player_counters(board, player_c))\n", "\n", " print \"Do you want to play again (y/n): \", \n", " again = raw_input()\n", " if again == 'y':\n", " continue;\n", " else:\n", " break \n", " if (no_of_moves < SIZE*SIZE and invalid_moves < 2):\n", " continue\n", " else:\n", " break\n", " if again == 'n':\n", " break\n", "print \"\\nGoodbye\\n\"" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "REVERSI\n", "You can go first on the first game, then we will take turns.\n", "You will be white - (0)\n", "I will be black - (@). \n", "Select a square for your move by typing a digit for the row and a letter for the column with no spaces between.\n", "Good luck! Press Enter to start.\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "a b c d e f \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 1|\n", " | | | | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 2|\n", " | | | | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 3|\n", " | | 0 | @ | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 4|\n", " | | @ | 0 | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 5|\n", " | | | | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 6|\n", " | | | | | | \n", " +\n", "---+ ---+ ---+ ---+ ---+ ---+ \n", "Please enter your move (row column - no space): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "row: 3\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "col: e\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", "a b c d e f \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 1|\n", " | | | | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 2|\n", " | | | | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 3|\n", " | | 0 | 0 | 0 | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 4|\n", " | | @ | 0 | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 5|\n", " | | | | | | \n", " + ---+ ---+ ---+ ---+ ---+ ---+ \n", " 6|\n", " | | | | | | \n", " +\n", "---+ ---+ ---+ ---+ ---+ ---+ \n", "The final score is: \n", "Computer 1 User 4 \n", "Do you want to play again (y/n): " ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "n\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \n", "Goodbye\n", "\n" ] } ], "prompt_number": 1 } ], "metadata": {} } ] }