summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter10.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'C_Programming:_A_Modern_Approach_by_K.N._King/Chapter10.ipynb')
-rwxr-xr-xC_Programming:_A_Modern_Approach_by_K.N._King/Chapter10.ipynb755
1 files changed, 755 insertions, 0 deletions
diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter10.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter10.ipynb
new file mode 100755
index 00000000..dcc36e8b
--- /dev/null
+++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter10.ipynb
@@ -0,0 +1,755 @@
+{
+ "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 (guess<secret_number):\n",
+ " print \"Too low, try again\"\n",
+ " else:\n",
+ " print \"Too high, try again\"\n",
+ "print \"Guess the secret number between 1 and %d.\"%MAX_NUMBER\n",
+ "print \"\"\n",
+ "secret_number=choose_new_secret_number()\n",
+ "print \"A new number has been chosen.\"\n",
+ "read_guesses()\n",
+ "command=raw_input( \"Play again? (Y/N) \")\n",
+ "while(command=='y' or command=='Y'):\n",
+ " secret_number=choose_new_secret_number()\n",
+ " print \"A new number has been chosen\"\n",
+ " read_guesses()\n",
+ " command=raw_input( \"Play again? (Y/N) \") \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Guess the secret number between 1 and 100.\n",
+ "\n",
+ "A new number has been chosen.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 55\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 88\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too high, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 75\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 80\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 85\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 86\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have won in 6 guesses: \n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Play again? (Y/N) y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "A new number has been chosen\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 50\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 70\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 90\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 95\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 99\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too high, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 97\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 98\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have won in 7 guesses: \n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Play again? (Y/N) n\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example guess2.c, Page 226"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import random\n",
+ "MAX_NUMBER=100\n",
+ "#def initialize_number_generator():\n",
+ "def new_secret_number():\n",
+ " return (random.randint(1,MAX_NUMBER))#generate random number\n",
+ "def read_guesses(secret_number):\n",
+ " num_guesses=0\n",
+ " while(1): #guesses\n",
+ " num_guesses=num_guesses+1\n",
+ " guess=int(raw_input(\"Enter guess: \"))\n",
+ " if(guess==secret_number):\n",
+ " print \"You have won in %d guesses: \"%num_guesses\n",
+ " print \"\"\n",
+ " return\n",
+ " elif (guess<secret_number):\n",
+ " print \"Too low, try again\"\n",
+ " else:\n",
+ " print \"Too high, try again\"\n",
+ "print \"Guess the secret number between 1 and %d.\"%MAX_NUMBER\n",
+ "print \"\"\n",
+ "secret_number=new_secret_number()\n",
+ "print \"A new number has been chosen.\"\n",
+ "read_guesses(secret_number)\n",
+ "command=raw_input( \"Play again? (Y/N) \")\n",
+ "while(command=='y' or command=='Y'):\n",
+ " secret_number=new_secret_number()\n",
+ " print \"A new number has been chosen\"\n",
+ " read_guesses(secret_number)\n",
+ " command=raw_input( \"Play again? (Y/N) \") \n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Guess the secret number between 1 and 100.\n",
+ "\n",
+ "A new number has been chosen.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 55\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 88\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too high, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 66\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 77\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too low, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 83\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too high, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 80\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too high, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 79\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Too high, try again\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter guess: 78\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "You have won in 8 guesses: \n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Play again? (Y/N) n\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example poker.c, Page 233"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "pairs=0\n",
+ "\n",
+ "def read_cards():\n",
+ " cards_read=0\n",
+ " card_exists = [[False for i in xrange(NUM_SUITS)] for i in xrange(NUM_RANKS)]\n",
+ " while(cards_read<NUM_CARDS):\n",
+ " bad_card=False\n",
+ " cardd=raw_input(\"Enter a card\")\n",
+ " a = cardd.split()\n",
+ " rank_ch=a[0]\n",
+ " suit_ch=a[1]\n",
+ " if (rank_ch=='0'):\n",
+ " exit(EXIT_SUCCESS)\n",
+ " elif(rank_ch=='2'):\n",
+ " rank=0\n",
+ " elif(rank_ch=='3'):\n",
+ " rank=1\n",
+ " elif(rank_ch=='4'):\n",
+ " rank=2\n",
+ " elif(rank_ch=='5'):\n",
+ " rank=3\n",
+ " elif(rank_ch=='6'):\n",
+ " rank=4\n",
+ " elif(rank_ch=='7'):\n",
+ " rank=5\n",
+ " elif(rank_ch=='8'):\n",
+ " rank=6\n",
+ " elif(rank_ch=='9'):\n",
+ " rank=7\n",
+ " elif(rank_ch=='t' or rank_ch=='T'):\n",
+ " rank=8\n",
+ " elif(rank_ch=='j' or rank_ch=='J'):\n",
+ " rank=9\n",
+ " elif(rank_ch=='q' or rank_ch=='Q'):\n",
+ " rank=10\n",
+ " elif(rank_ch=='k' or rank_ch=='K'):\n",
+ " rank=11\n",
+ " elif(rank_ch=='a' or rank_ch=='A'):\n",
+ " rank=12\n",
+ " else:\n",
+ " bad_card=True\n",
+ " if(suit_ch=='c'or suit_ch=='C'):\n",
+ " suit=0\n",
+ " elif(suit_ch=='d' or suit_ch=='D'):\n",
+ " suit=1\n",
+ " elif(suit_ch=='h' or suit_ch=='H'):\n",
+ " suit=1\n",
+ " elif(suit_ch=='s' or suit_ch=='S'):\n",
+ " suit=1\n",
+ " else:\n",
+ " bad_card=True\n",
+ " if(bad_card):\n",
+ " print \"Bad card; ignored\"\n",
+ " elif (card_exists[rank][suit]):\n",
+ " print \"Duplicate card; ignored\"\n",
+ " else:\n",
+ " num_in_rank[rank]=num_in_rank[rank]+1\n",
+ " num_in_suit[suit]=num_in_suit[suit]+1\n",
+ " card_exists[rank][suit]=True\n",
+ " cards_read=cards_read+1\n",
+ "\n",
+ "def analyse_hand():\n",
+ " num_consec=0\n",
+ " for suit in range(NUM_SUITS):\n",
+ " if(num_in_suit[suit]==NUM_CARDS):\n",
+ " flush=True\n",
+ " rank=0\n",
+ " while(num_in_rank[rank]==0):\n",
+ " rank=rank+1\n",
+ " while(rank<NUM_RANKS and num_in_rank[rank]>0):\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": {}
+ }
+ ]
+} \ No newline at end of file