{ "metadata": { "name": "", "signature": "sha256:89fb3fef5984e311e1eb6ac1f3a0c24d3e369a1266fe562ec873d32c24f96971" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 10: Program Organization" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page 219" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def sum_digits(n):\n", " sum = 0 #local variable\n", " while(n>0):\n", " sum =sum+(n%10)\n", " n=n/10\n", " return sum\n", "#sum_digits(10)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "1" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example on Page 222" ] }, { "cell_type": "code", "collapsed": false, "input": [ "STACK_SIZE=100\n", "contents=[None]*STACK_SIZE\n", "top=0\n", "def make_empty():\n", " top=0\n", "def is_empty():\n", " return top==0\n", "def is_full():\n", " return top==STACK_SIZE\n", "def push(i):\n", " if(is_full()):\n", " call_stackoverflow=0\n", " #stack_overflow()\n", " else:\n", " contents[top+1]=i\n", "def pop():\n", " if(is_empty()):\n", " call_stackoverflow=0\n", " #stack_underflow()\n", " else:\n", " return contents[top-1]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example guess.c, Page 224" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "MAX_NUMBER=100\n", "#def initialize_number_generator():\n", "def choose_new_secret_number():\n", " secret_number=random.randint(1,MAX_NUMBER) #generate random number\n", " return secret_number\n", "def read_guesses():\n", " num_guesses=0\n", " while(1):\n", " num_guesses=num_guesses+1\n", " guess=int(raw_input(\"Enter guess: \")) #guesses\n", " if(guess==secret_number):\n", " print \"You have won in %d guesses: \"%num_guesses\n", " print \"\"\n", " return\n", " elif (guess0):\n", " num_consec=num_consec+1\n", " rank=rank+1\n", " if(num_consec==NUM_CARDS):\n", " straight=True\n", " return\n", " for rank in range(NUM_RANKS):\n", " if(num_in_rank[rank]==4):\n", " four=True\n", " if(num_in_rank[rank]==3):\n", " three=True\n", " if(num_in_rank[rank]==2):\n", " pairs=pairs+1\n", " \n", "def print_result():\n", " if(straight==True and flush==True):\n", " print \"Straight flush\"\n", " elif(four):\n", " print \"Four of a kind\"\n", " elif(three==True and pairs==1):\n", " print \"Full house\"\n", " elif(flush):\n", " print \"Flush\"\n", " elif(straight):\n", " print \"Straight\"\n", " elif(three):\n", " print \"Three of a kind\"\n", " elif(pairs==2):\n", " print \"Two pairs\"\n", " elif(pairs==1):\n", " print \"Pair\"\n", " else:\n", " print \"High card\"\n", " print \"\"\n", " \n", "while(1):\n", " straight=False\n", " flush=False\n", " four=False\n", " three=False\n", " NUM_RANKS=13\n", " NUM_SUITS=4\n", " NUM_CARDS=5\n", " num_in_rank=[0]*NUM_RANKS\n", " num_in_suit=[0]*NUM_SUITS\n", "\n", " read_cards()\n", " analyse_hand()\n", " print_result()\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a card2 s\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a card5 s\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a card4 s\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a card3 s\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter a card6 s\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "High card\n", "\n" ] } ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }