diff options
Diffstat (limited to 'C_Programming:_A_Modern_Approach_by_K.N._King')
22 files changed, 4992 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 diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter11.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter11.ipynb new file mode 100755 index 00000000..ce091d42 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter11.ipynb @@ -0,0 +1,81 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:74e91fb616db3aa96bac711fa075c7c19a951b52d2e8ba98c61929ac216b2aca"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 11: Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example maxmin.c, Page 250"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "N=10\n",
+ "def max_min(a,n): #function definition\n",
+ " max=a[0]\n",
+ " min=a[0]\n",
+ " for i in range(n-1): #calculating max and min from list\n",
+ " if(a[i+1]>max):\n",
+ " max=a[i+1]\n",
+ " elif(a[i+1]<min):\n",
+ " min=a[i+1]\n",
+ " return (max,min)\n",
+ "li=raw_input(\"Enter %d numbers: \" % N) #input of numbers from user\n",
+ "b=map(int, li.split())\n",
+ "big,small=max_min(b,N) #call function to calculate max and min\n",
+ "print \"Largest: %d\"%big #print max and min\n",
+ "print \"Smallest: %d\"%small\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Largest: 102\n",
+ "Smallest: 7\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter12.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter12.ipynb new file mode 100755 index 00000000..a6dddf65 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter12.ipynb @@ -0,0 +1,71 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:2d5527cee2d14f67c2f5fd4cdfcbb27ae6cb62aa3eec37f9815dae10c6c114db"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 12: Pointers and Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example reverse3.c, Page 264"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "N=10\n",
+ "li = raw_input(\"Enter %d numbers: \" % N) #accepting input in list\n",
+ "a = map(int, li.split()) \n",
+ "print \"In reverse order: \",\n",
+ "for i in a[::-1]: #print reversed list\n",
+ " print i,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In reverse order: 31 50 11 23 94 7 102 49 82 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter13.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter13.ipynb new file mode 100755 index 00000000..03c83450 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter13.ipynb @@ -0,0 +1,127 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:00fb09fcd8819c39a88f9cac94eaa93d1e9e72c864c95cc17c2c19aa93c94143"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 13: Strings"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example remind.c, Page 294"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX_REMIND=50\n",
+ "MSG_LEN=60\n",
+ "reminders = [['' for i in xrange(MSG_LEN+3)] for i in xrange(MAX_REMIND)]\n",
+ "num_remind=0\n",
+ "while(1):\n",
+ " if(num_remind==MAX_REMIND):\n",
+ " print \"--No space left --\"\n",
+ " break\n",
+ " li=raw_input( \"Enter day and reminder: \")\n",
+ " li1=li.split()\n",
+ " day_str=li1[0]\n",
+ " if(day_str=='0'):\n",
+ " break\n",
+ " msg_str=li1[1:]\n",
+ " i=0\n",
+ " for i in range(num_remind):\n",
+ " if(day_str<reminders[i]):\n",
+ " break\n",
+ " j=num_remind\n",
+ " while(j>i):\n",
+ " reminders[j]=reminders[j-1]\n",
+ " j=j-1\n",
+ " reminders[i]=day_str\n",
+ " reminders[i]=str(reminders[i])+str(msg_str)\n",
+ " \n",
+ " num_remind=num_remind+1\n",
+ "print \"\"\n",
+ "print \"Day Reminder\"\n",
+ "for i in range(num_remind):\n",
+ " print \"%s\"%reminders[i]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example planet.c, Page 303"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NUM_PLANETS=9\n",
+ "li=raw_input()\n",
+ "argv=li.split()\n",
+ "planets=['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune','Pluto']\n",
+ "argc=len(argv)\n",
+ "for i in range(argc-1):\n",
+ " for j in range(NUM_PLANETS):\n",
+ " if( argv[i+1]==planets[j]):\n",
+ " print \"%s is a planet %d\"%(argv[i+1],(j+1))\n",
+ " break\n",
+ " if(argv[i] not in planets):\n",
+ " print \"%s is not a planet\"%argv[i]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "planet Jupiter Venus Earth fred\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Jupiter is a planet 5\n",
+ "planet is not a planet\n",
+ "Venus is a planet 2\n",
+ "Earth is a planet 3\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter15.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter15.ipynb new file mode 100755 index 00000000..8429721c --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter15.ipynb @@ -0,0 +1,83 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:66fb80b57a6473ffca1e8f425382d8ae7bda3cbb5a42b72a09573b1379da5aa8"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 15: Writing Large Programs"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example justify.c, Page 359\n",
+ "Other files- word.h, line.h, word.c, line.c"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import textwrap\n",
+ "quote2=''\n",
+ "quote= (' C is quirky, flawed, and an '\n",
+ " 'enormous success. Although accidents of history '\n",
+ " ' surely helped, it evidently satisfied a need '\n",
+ " ''\n",
+ " 'for a system implementation language efficient '\n",
+ " 'enough to displace assembly language, '\n",
+ " ' yet sufficiently abstract and fluent to describe '\n",
+ " ' algorithms and interactions in a wide variety '\n",
+ " 'of environments.'\n",
+ " ' -- Dennis M. Ritchie')\n",
+ "for i in quote:\n",
+ " if (len(i)>20):\n",
+ " i=(i[:20] + '*')\n",
+ " quote2=quote2+i\n",
+ "quote2=' '.join(quote2.split()) \"\"\"Python has inbuilt functions for removing \n",
+ "white spaces and textwrap, justifying text hence reduces the amount of code as required in C\"\"\"\n",
+ "quotee= textwrap.fill(quote2)\n",
+ "print quotee"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "C is quirky, flawed, and an enormous success. Although accidents of\n",
+ "history surely helped, it evidently satisfied a need for a system\n",
+ "implementation language efficient enough to displace assembly\n",
+ "language, yet sufficiently abstract and fluent to describe algorithms\n",
+ "and interactions in a wide variety of environments. -- Dennis M.\n",
+ "Ritchie\n"
+ ]
+ }
+ ],
+ "prompt_number": 16
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter16.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter16.ipynb new file mode 100755 index 00000000..20e3b1e5 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter16.ipynb @@ -0,0 +1,180 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:9fe9374bd25aac8dfc98eab84988bb008dcfb464faf94260bebab61ae764fb98"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 16: Structures, Unions and Enumerations"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example inventory.c, Page 391"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NAME_LEN=25\n",
+ "MAX_PARTS=100\n",
+ "num_parts=0\n",
+ "number1=[0]*100\n",
+ "on_hand1=[0]*100\n",
+ "name1=[0]*100\n",
+ "def find_part(number):\n",
+ " i=0\n",
+ " for i in range (i,num_parts):\n",
+ " if(number1[i]==number):\n",
+ " return i\n",
+ " return -1\n",
+ "def insert():\n",
+ " num_parts=0\n",
+ " if(num_parts==MAX_PARTS):\n",
+ " print \"Database is full, cant add more parts\"\n",
+ " return\n",
+ " part_number=int(raw_input(\"Enter part number: \"))\n",
+ " if(find_part(part_number)>=0):\n",
+ " print \"Part already exists\"\n",
+ " return\n",
+ " number1[num_parts]=part_number\n",
+ " name1[num_parts]=raw_input(\"Enter part name: \")\n",
+ " on_hand1[num_parts]=raw_input(\"Enter quality on hand: \")\n",
+ " num_parts=num_parts+1\n",
+ " \n",
+ "def search():\n",
+ " number=int(raw_input(\"Enter part number: \"))\n",
+ " i=find_part(number)\n",
+ " if(i>=0):\n",
+ " print \"Part name: %s\" %name1[i]\n",
+ " print \"Quantity on hand: %d\"%on_hand1[i]\n",
+ " else:\n",
+ " print \"Part not found\"\n",
+ " \n",
+ "def update():\n",
+ " number=int(raw_input(\"Enter part number: \"))\n",
+ " i=find_part(number)\n",
+ " if(i>=0):\n",
+ " change=int(raw_input(\"Enter change in quantity on hand: \")) \n",
+ " on_hand1[i]=on_hand1[i]+change\n",
+ " else:\n",
+ " print \"Part not found\"\n",
+ "\n",
+ "while(1):\n",
+ " code=raw_input(\"Enter operation code: \")\n",
+ " if code=='i':\n",
+ " insert()\n",
+ " break\n",
+ " elif code=='s':\n",
+ " search()\n",
+ " break\n",
+ " elif code=='u':\n",
+ " update()\n",
+ " break\n",
+ " elif(code=='p'):\n",
+ " print()\n",
+ " break\n",
+ "\n",
+ "print \"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter operation code: i\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter part number: 528\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter part name: Disk Drive\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter quality on hand: 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example readline.c, Page 395"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def read_line(str,n):\n",
+ " ch=0\n",
+ " i=0\n",
+ " EOF='\\n'\n",
+ " ch=raw_input()\n",
+ " while(isspace(ch)):\n",
+ " donothing=0\n",
+ " while(ch!='\\n' and ch!=EOF):\n",
+ " if(i<n):\n",
+ " str[i+1]=ch\n",
+ " ch=raw_input()\n",
+ " str[i]='\\0'\n",
+ " return i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter17.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter17.ipynb new file mode 100755 index 00000000..f25aa999 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter17.ipynb @@ -0,0 +1,268 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:8ebda07d5e13c6b4a8132b67344e9246eeb8703f2dcc433e1b4e350db4cace07"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 17: Advanced Uses of Pointers"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example remind2.c, Page 418"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "MAX_REMIND=50\n",
+ "MSG_LEN=60\n",
+ "reminders = [['' for i in xrange(MSG_LEN+3)] for i in xrange(MAX_REMIND)]\n",
+ "num_remind=0\n",
+ "while(1):\n",
+ " if(num_remind==MAX_REMIND):\n",
+ " print \"--No space left --\"\n",
+ " break\n",
+ " li=raw_input( \"Enter day and reminder: \")\n",
+ " li1=li.split()\n",
+ " day_str=li1[0]\n",
+ " if(day_str=='0'):\n",
+ " breakb n\n",
+ " msg_str=li1[1:]\n",
+ " i=0\n",
+ " for i in range(num_remind):\n",
+ " if(day_str<reminders[i]):\n",
+ " break\n",
+ " j=num_remind\n",
+ " while(j>i):\n",
+ " reminders[j]=reminders[j-1]\n",
+ " j=j-1\n",
+ " reminders[i]=day_str\n",
+ " reminders[i]=str(reminders[i])+str(msg_str)\n",
+ " \n",
+ " num_remind=num_remind+1\n",
+ "print \"\"\n",
+ "print \"Day Reminder\"\n",
+ "for i in range(num_remind):\n",
+ " print \"%s\"%reminders[i]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example inventory2.c, Page 434"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NAME_LEN=25\n",
+ "MAX_PARTS=100\n",
+ "num_parts=0\n",
+ "number1=[0]*100\n",
+ "on_hand1=[0]*100\n",
+ "name1=[0]*100\n",
+ "def find_part(number):\n",
+ " i=0\n",
+ " for i in range (i,num_parts):\n",
+ " if(number1[i]==number):\n",
+ " return i\n",
+ " return -1\n",
+ "def insert():\n",
+ " num_parts=0\n",
+ " if(num_parts==MAX_PARTS):\n",
+ " print \"Database is full, cant add more parts\"\n",
+ " return\n",
+ " part_number=int(raw_input(\"Enter part number: \"))\n",
+ " if(find_part(part_number)>=0):\n",
+ " print \"Part already exists\"\n",
+ " return\n",
+ " number1[num_parts]=part_number\n",
+ " name1[num_parts]=raw_input(\"Enter part name: \")\n",
+ " on_hand1[num_parts]=raw_input(\"Enter quality on hand: \")\n",
+ " num_parts=num_parts+1\n",
+ " \n",
+ "def search():\n",
+ " number=int(raw_input(\"Enter part number: \"))\n",
+ " i=find_part(number)\n",
+ " if(i>=0):\n",
+ " print \"Part name: %s\" %name1[i]\n",
+ " print \"Quantity on hand: %d\"%on_hand1[i]\n",
+ " else:\n",
+ " print \"Part not found\"\n",
+ " \n",
+ "def update():\n",
+ " number=int(raw_input(\"Enter part number: \"))\n",
+ " i=find_part(number)\n",
+ " if(i>=0):\n",
+ " change=int(raw_input(\"Enter change in quantity on hand: \")) \n",
+ " on_hand1[i]=on_hand1[i]+change\n",
+ " else:\n",
+ " print \"Part not found\"\n",
+ "\n",
+ "while(1):\n",
+ " code=raw_input(\"Enter operation code: \")\n",
+ " if code=='i':\n",
+ " insert()\n",
+ " break\n",
+ " elif code=='s':\n",
+ " search()\n",
+ " break\n",
+ " elif code=='u':\n",
+ " update()\n",
+ " break\n",
+ " elif(code=='p'):\n",
+ " print()\n",
+ " break\n",
+ "\n",
+ "print \"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tabulate.c, Page 444"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import math\n",
+ "\n",
+ "initial=float(raw_input(\"Enter initial value: \"))\n",
+ "final=float(raw_input(\"Enter final value: \"))\n",
+ "increment=float(raw_input(\"Enter increment: \"))\n",
+ "print \"\"\n",
+ "print \" x cos(x)\"\n",
+ "print \"-------------------------\"\n",
+ "\n",
+ "def tabulatec( first, last, incr):\n",
+ " num_intervals=int(math.ceil((last-first)/incr))\n",
+ " for i in range(num_intervals):\n",
+ " x=first+i*incr\n",
+ " print \"%10.5f %10.5f\"%(x,math.cos(x))\n",
+ "def tabulates( first, last, incr):\n",
+ " num_intervals=int(math.ceil((last-first)/incr))\n",
+ " for i in range(num_intervals):\n",
+ " x=first+i*incr\n",
+ " print \"%10.5f %10.5f\"%(x,math.sin(x))\n",
+ "def tabulatet( first, last, incr):\n",
+ " num_intervals=int(math.ceil((last-first)/incr))\n",
+ " for i in range(num_intervals):\n",
+ " x=first+i*incr\n",
+ " print \"%10.5f %10.5f\"%(x,math.tan(x))\n",
+ "\n",
+ " \n",
+ "tabulatec(initial,final, increment)\n",
+ "print \"\"\n",
+ "print \" x sin(x)\"\n",
+ "print \"\"\n",
+ "print \"-------------------------\"\n",
+ "tabulates(initial,final, increment)\n",
+ "print \"\"\n",
+ "print \" x tan(x)\"\n",
+ "print \"\"\n",
+ "print \"-------------------------\"\n",
+ "tabulatet(initial,final, increment)\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter initial value: 0\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter final value: .5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter increment: .1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ " x cos(x)\n",
+ "--------------------------\n",
+ " 0.00000 1.00000\n",
+ " 0.10000 0.99500\n",
+ " 0.20000 0.98007\n",
+ " 0.30000 0.95534\n",
+ " 0.40000 0.92106\n",
+ "\n",
+ " x sin(x)\n",
+ "\n",
+ "-------------------------\n",
+ " 0.00000 0.00000\n",
+ " 0.10000 0.09983\n",
+ " 0.20000 0.19867\n",
+ " 0.30000 0.29552\n",
+ " 0.40000 0.38942\n",
+ "\n",
+ " x tan(x)\n",
+ "\n",
+ "-------------------------\n",
+ " 0.00000 0.00000\n",
+ " 0.10000 0.10033\n",
+ " 0.20000 0.20271\n",
+ " 0.30000 0.30934\n",
+ " 0.40000 0.42279\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter19.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter19.ipynb new file mode 100755 index 00000000..42d5374c --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter19.ipynb @@ -0,0 +1,322 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:6b5cf549b3fdac88be0a759d2fc29d02814154da669e9ab30e73ad0c8e2bbf9e"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 19: Program Design"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example stack1.c, Page 488. Other files- stack.h\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "STACK_SIZE=100\n",
+ "contents=[None]*STACK_SIZE\n",
+ "top=0\n",
+ "def terminate(message):\n",
+ " print message\n",
+ " sys.exit()\n",
+ "def make_empty():\n",
+ " top=0\n",
+ "def is_empty():\n",
+ " if(top==0):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def is_full():\n",
+ " if(top==STACK_SIZE):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def push(i):\n",
+ " if(is_full()):\n",
+ " terminate(\"Error in push: stack is full.\")\n",
+ " contents[top+1]=i\n",
+ "def pop():\n",
+ " if(is_empty()):\n",
+ " terminate(\"Error in push: stack is empty.\")\n",
+ " return contents[top-1]\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example stack2.c, Page 489"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "STACK_SIZE=100\n",
+ "contents=[None]*STACK_SIZE\n",
+ "top=0\n",
+ "def terminate(message):\n",
+ " print message\n",
+ " sys.exit()\n",
+ "def make_empty():\n",
+ " top=0\n",
+ "def is_empty():\n",
+ " if(top==0):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def is_full():\n",
+ " if(top==STACK_SIZE):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def push(i):\n",
+ " if(is_full()):\n",
+ " terminate(\"Error in push: stack is full.\")\n",
+ " contents[top+1]=i\n",
+ "def pop():\n",
+ " if(is_empty()):\n",
+ " terminate(\"Error in push: stack is empty.\")\n",
+ " return contents[top-1]\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example stackclient.c, Page 494"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "class Stack:\n",
+ " def __init__(self):\n",
+ " self.items = []\n",
+ " def is_empty(self):\n",
+ " return self.items == []\n",
+ " def push(self, item):\n",
+ " self.items.append(item)\n",
+ " def pop(self):\n",
+ " return self.items.pop()\n",
+ " def size(self):\n",
+ " return len(self.items)\n",
+ " def make_empty(self):\n",
+ " while len(self.items) > 0 : self.items.pop()\n",
+ "s1=Stack()\n",
+ "s2=Stack()\n",
+ "s1.push(1)\n",
+ "s1.push(2)\n",
+ "n=s1.pop()\n",
+ "print \"Popped %d from s1\"%n\n",
+ "s2.push(n)\n",
+ "n=s1.pop()\n",
+ "print \"Popped %d from s1\"%n\n",
+ "s2.push(n)\n",
+ "while(s2.is_empty()!=True):\n",
+ " print \"Popped %d from s2\"%s2.pop()\n",
+ "s2.push(3)\n",
+ "s2.make_empty()\n",
+ "if(s2.is_empty()):\n",
+ " print \"s2 is empty\"\n",
+ "else:\n",
+ " print \"s2 is not empty\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Popped 2 from s1\n",
+ "Popped 1 from s1\n",
+ "Popped 1 from s2\n",
+ "Popped 2 from s2\n",
+ "s2 is empty\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example stackADT.c, Page 495"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "STACK_SIZE=100\n",
+ "contents=[None]*STACK_SIZE\n",
+ "top=0\n",
+ "def terminate(message):\n",
+ " print message\n",
+ " sys.exit()\n",
+ "def make_empty():\n",
+ " top=0\n",
+ "def is_empty():\n",
+ " if(top==0):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def is_full():\n",
+ " if(top==STACK_SIZE):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def push(i):\n",
+ " if(is_full()):\n",
+ " terminate(\"Error in push: stack is full.\")\n",
+ " contents[top+1]=i\n",
+ "def pop():\n",
+ " if(is_empty()):\n",
+ " terminate(\"Error in push: stack is empty.\")\n",
+ " return contents[top-1]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example stackADT2.c, Page 498"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#similar as above since Python doesn't have pointers\n",
+ "import sys\n",
+ "STACK_SIZE=100\n",
+ "contents=[None]*STACK_SIZE\n",
+ "top=0\n",
+ "def terminate(message):\n",
+ " print message\n",
+ " sys.exit()\n",
+ "def make_empty():\n",
+ " top=0\n",
+ "def is_empty():\n",
+ " if(top==0):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def is_full():\n",
+ " if(top==STACK_SIZE):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def push(i):\n",
+ " if(is_full()):\n",
+ " terminate(\"Error in push: stack is full.\")\n",
+ " contents[top+1]=i\n",
+ "def pop():\n",
+ " if(is_empty()):\n",
+ " terminate(\"Error in push: stack is empty.\")\n",
+ " return contents[top-1]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example stackADT3.c, Page 500"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#similar as above since Python doesn't have pointers\n",
+ "import sys\n",
+ "STACK_SIZE=100\n",
+ "contents=[None]*STACK_SIZE\n",
+ "top=0\n",
+ "def terminate(message):\n",
+ " print message\n",
+ " sys.exit()\n",
+ "def make_empty():\n",
+ " top=0\n",
+ "def is_empty():\n",
+ " if(top==0):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def is_full():\n",
+ " if(top==STACK_SIZE):\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "def push(i):\n",
+ " if(is_full()):\n",
+ " terminate(\"Error in push: stack is full.\")\n",
+ " contents[top+1]=i\n",
+ "def pop():\n",
+ " if(is_empty()):\n",
+ " terminate(\"Error in push: stack is empty.\")\n",
+ " return contents[top-1]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 9
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter2.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter2.ipynb new file mode 100755 index 00000000..c925dc1b --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter2.ipynb @@ -0,0 +1,237 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:1970948843f0abe57577274c22e34507ff26124029c8f4289139bc598d62eb66"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 2: C Fundamentals"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example pun.c on Page 10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " print \"To C, or not to C: that is the question.\" #print statement demo\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "To C, or not to C: that is the question.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page 14"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#ways for printing on a single line\n",
+ "print \"To C, or not to C: that is the question.\" \n",
+ "print \"To C, or not to C:\",\n",
+ "print \"that is the question.\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "To C, or not to C: that is the question.\n",
+ "To C, or not to C: that is the question.\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example dweight.c on Page 20"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " #variable declaration\n",
+ " height = 8 \n",
+ " length = 12\n",
+ " width =10\n",
+ " volume = height * length * width #volume calculation\n",
+ " weight = (volume + 165)/166 #weight calculation\n",
+ " \n",
+ " #print statements\n",
+ " print \"Dimensions: %dx%dx%d\" % (length,width,height) \n",
+ " print \"Volume (cubic inches): %d\" % volume\n",
+ " print \"Dimensional weight (pounds): %d\" % weight\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Dimensions: 12x10x8\n",
+ "Volume (cubic inches): 960\n",
+ "Dimensional weight (pounds): 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 15
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example dweight2.c on Page 23"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " #input from user\n",
+ " height = int(raw_input(\"Enter height of box: \"))\n",
+ " length = int(raw_input(\"Enter length of box: \"))\n",
+ " width = int(raw_input(\"Enter width of box: \"))\n",
+ " volume = height * length * width #volume calculation\n",
+ " weight = (volume + 165)/166 #weight calculation\n",
+ " \n",
+ " #print statements\n",
+ " print \"Volume (cubic inches): %d\" % volume\n",
+ " print \"Dimensional weight (pounds): %d\" % weight\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter height of box: 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter length of box: 12\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter width of box: 10\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Volume (cubic inches): 960\n",
+ "Dimensional weight (pounds): 6\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example celsius.c on Page 24"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " #variable declaration\n",
+ " FREEZING_PT=32.0\n",
+ " SCALE_FACTOR= 5.02/9.0\n",
+ " \n",
+ " #input from user\n",
+ " farenheit=float(raw_input(\"Enter Farenheit temperature: \"))\n",
+ " celsius=(farenheit-FREEZING_PT) * SCALE_FACTOR #convert farenheit to celcius\n",
+ " print \"Celsius equivalent: %.1f\" % celsius\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter Farenheit temperature: 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Celsius equivalent: 37.9\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter20.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter20.ipynb new file mode 100755 index 00000000..10d6049f --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter20.ipynb @@ -0,0 +1,161 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:4a14f929c77abfd0a54b26e2baa48a2c7f478d22968a4f6231afc128a12dd480"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 20: Low-Level Programming"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example xor.c, Page 515"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "KEY='&'\n",
+ "orig=raw_input()\n",
+ "orig_char=list(orig)\n",
+ "new_char=[]\n",
+ "\n",
+ "def sxor(s1,s2): \n",
+ " return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))\n",
+ "for i in orig_char:\n",
+ " N=sxor(i,KEY)\n",
+ " new_char.append(N)\n",
+ "print ''.join(new_char)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Trust not him with your secrets, who, when left alone in your room, turns over your papers. Johann Kaspar Lavater \n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "rTSUR\u0006HIR\u0006NOK\u0006QORN\u0006_IST\u0006UCETCRU\n",
+ "\u0006QNI\n",
+ "\u0006QNCH\u0006JC@R\u0006GJIHC\u0006OH\u0006_IST\u0006TIIK\n",
+ "\u0006RSTHU\u0006IPCT\u0006_IST\u0006VGVCTU\b\u0006lINGHH\u0006mGUVGT\u0006jGPGRCT\u0006\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example viewmemory.c, Page 521"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "from itertools import product\n",
+ "def main():\n",
+ " \n",
+ " addr=\"\"\n",
+ " i=0\n",
+ " n=0\n",
+ " print \"Address of main function: %s\" % hex(id(main))\n",
+ " print \"Address of addr variable: %s\" % hex(id(addr))\n",
+ " addr=raw_input( \"Enter a (hex) address: \")\n",
+ " n=int(raw_input( \"Enter number of bytes to view: \"))\n",
+ " #printf(\"\\n\");\n",
+ " print \" Address Bytes Characters\\n\"\n",
+ " print \" - - - - - - - - - - - - - - - - - - - - - - \"\n",
+ " ptr = addr;\n",
+ " for nn in range(n,0,-10):\n",
+ " print \"%8s \"% hex(id(addr))\n",
+ " # for i,i in product(range(0//10), range(0//n)) :\n",
+ " #print \"%.2X \", *(ptr + i)); \n",
+ " #for (; i <10; i++)\n",
+ " # printf(\" \"); #pointer dereferencing not in python\n",
+ " #printf(\" \");\n",
+ " #for (i = 0; i < 10 && i < n; i++) {\n",
+ " # BYTE ch = *(ptr + i);\n",
+ " #if (!isprint(ch))\n",
+ " # ch = '.';\n",
+ " # printf(\"%c\", ch);\n",
+ " #}\n",
+ " #printf(\"\\n\");\n",
+ " #ptr += 10;\n",
+ " #} \n",
+ " #return 0;\n",
+ " #}\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Address of main function: 0x4117eb8L\n",
+ "Address of addr variable: 0x1c3c148L\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a (hex) address: 8048000\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of bytes to view: 40\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Address Bytes Characters\n",
+ "\n",
+ " - - - - - - - - - - - - - - - - - - - - - - \n",
+ "0x41884b8L \n",
+ "0x41884b8L \n",
+ "0x41884b8L \n",
+ "0x41884b8L \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter22.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter22.ipynb new file mode 100755 index 00000000..e47d926c --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter22.ipynb @@ -0,0 +1,158 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:61ba7fc12caccdde58e5c478fe47afe52c6e4084264f0cb759e096ea4f746518"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 22: File Operations"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example canopen.c, Page 547"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "try:\n",
+ " fname=raw_input()\n",
+ " fp=open('noexist',r)\n",
+ "except:\n",
+ " print \"Can't be opened\"\n",
+ "else:\n",
+ " print \"Can be opened\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "file\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Can't be opened\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example fcopy.c, Page 568"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "try:\n",
+ " s=raw_input(\"Source\")\n",
+ " d=raw_input(\"Destination\")\n",
+ " source_fp=open(s,'rb')\n",
+ " dest_fp = open(d,wb)\n",
+ "\n",
+ " for line in source_fp.readlines():\n",
+ " dest_fp.write(line) \n",
+ "\n",
+ " dest_fp.close()\n",
+ " source_fp.close()\n",
+ "except:\n",
+ " print \"Can't be opened\"\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Sourcef1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Destinationf2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Can't be opened\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example invclear.c, Page 574"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "NAME_LEN=25\n",
+ "max_parts=100\n",
+ "char name=None*(NAME_LEN+1)\n",
+ "on_hand=list()\n",
+ "try:\n",
+ " fp=open('inventory.dat','r')\n",
+ "except:\n",
+ " print \"Cant open inventory file\"\n",
+ "num_parts=fp.read()\n",
+ "for i in range (num_parts):\n",
+ " on_hand[i]=0\n",
+ "fp.seek(0,0)\n",
+ "fp.write(on_hand)\n",
+ "fp.close\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter23.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter23.ipynb new file mode 100755 index 00000000..d6c12ffe --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter23.ipynb @@ -0,0 +1,127 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:5233b8440acc7b2d62016ba76634e04bd09fd81a7f83bcd9847657fe204b0cea"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 23: Library Support for Numbers and Character Data"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tclassify.c, Page 613"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import string\n",
+ "print \" alnum alpha digit graph lower print punct space upper xdigit\"\n",
+ "p=\"azAZ0 !\\t\"\n",
+ "p1=list(p)\n",
+ "for i in p1:\n",
+ " listt=[\" \"]*10\n",
+ " print i,\n",
+ " if(i.isalnum()):\n",
+ " listt[0]=\" X\"\n",
+ " if(i.isalpha()):\n",
+ " listt[1]=\" X\"\n",
+ " if(i.isdigit()):\n",
+ " listt[2]=\" X\"\n",
+ " if(i.isspace()!=True):\n",
+ " listt[3]=\" X\"\n",
+ " if(i.islower()):\n",
+ " listt[4]=\" X\"\n",
+ " if(i in string.printable):\n",
+ " listt[5]=\" X\"\n",
+ " if(i in string.punctuation):\n",
+ " listt[6]=\" X\"\n",
+ " if(i.isspace()):\n",
+ " listt[7]=\" X\"\n",
+ " if(i.isupper()):\n",
+ " listt[8]=\" X\"\n",
+ " if(i in string.hexdigits):\n",
+ " listt[9]=\" X\"\n",
+ " print listt"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " alnum alpha digit graph lower print punct space upper xdigit\n",
+ "a [' X', ' X', ' ', ' X', ' X', ' X', ' ', ' ', ' ', ' X']\n",
+ "z [' X', ' X', ' ', ' X', ' X', ' X', ' ', ' ', ' ', ' ']\n",
+ "A [' X', ' X', ' ', ' X', ' ', ' X', ' ', ' ', ' X', ' X']\n",
+ "Z [' X', ' X', ' ', ' X', ' ', ' X', ' ', ' ', ' X', ' ']\n",
+ "0 [' X', ' ', ' X', ' X', ' ', ' X', ' ', ' ', ' ', ' X']\n",
+ " [' ', ' ', ' ', ' ', ' ', ' X', ' ', ' X', ' ', ' ']\n",
+ "! [' ', ' ', ' ', ' X', ' ', ' X', ' X', ' ', ' ', ' ']\n",
+ "\t[' ', ' ', ' ', ' ', ' ', ' X', ' ', ' X', ' ', ' ']\n"
+ ]
+ }
+ ],
+ "prompt_number": 38
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tcasemap.c, Page 615"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "p=\"aA0!\"\n",
+ "pl=list(p)\n",
+ "for i in pl:\n",
+ " print \"tolower('%c') is '%c'; \"% (i,i.lower()),\n",
+ " print \"toupper('%c') is '%c'; \"% (i,i.upper())"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "tolower('a') is 'a'; toupper('a') is 'A'; \n",
+ "tolower('A') is 'a'; toupper('A') is 'A'; \n",
+ "tolower('0') is '0'; toupper('0') is '0'; \n",
+ "tolower('!') is '!'; toupper('!') is '!'; \n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter24.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter24.ipynb new file mode 100755 index 00000000..05dd8488 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter24.ipynb @@ -0,0 +1,126 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:70182e1ed50aea78649f1bef2946ce2b6e335126b84963ba06fce0660ac72886"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 24: Error Handling"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tsignal.c, Page 634"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import signal\n",
+ "import sys\n",
+ "def handler(sig,frame):\n",
+ " print \"Handler called for signal\",sig\n",
+ "print \"Installing handler for signal\", signal.SIGINT\n",
+ "orig_handler=signal.signal(signal.SIGINT,handler)\n",
+ "print \"Changing handler to SIG_IGN\"\n",
+ "signal.signal(signal.SIGINT,signal.SIG_IGN)\n",
+ "print \"Restoring original handler\"\n",
+ "signal.signal(signal.SIGINT,orig_handler)\n",
+ "print \"Program terminates normally\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Installing handler for signal 2\n",
+ "Changing handler to SIG_IGN\n",
+ "Restoring original handler\n",
+ "Program terminates normally\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tsetjmp.c, Page 636"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " def f1():\n",
+ " print \"f1 begins\"\n",
+ " f2()\n",
+ " return\n",
+ " print \"f1 returns\"\n",
+ "\n",
+ " def f2():\n",
+ " print \"f2 begins\"\n",
+ " #longjmp(env, 1)\n",
+ " setjmp=1\n",
+ " print \"Program terminates: longjmp called\"\n",
+ " return\n",
+ " print \"f2 returns\"\n",
+ "\n",
+ " setjmp=0\n",
+ " if (setjmp == 0):\n",
+ " print \"setjmp returned 0\"\n",
+ " else:\n",
+ " print \"Program terminates: longjmp called\"\n",
+ " f1()\n",
+ " return\n",
+ " print \"Program terminates normally\"\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "setjmp returned 0\n",
+ "f1 begins\n",
+ "f2 begins\n",
+ "Program terminates: longjmp called\n"
+ ]
+ }
+ ],
+ "prompt_number": 18
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter26.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter26.ipynb new file mode 100755 index 00000000..17b301ac --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter26.ipynb @@ -0,0 +1,288 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:fbcb6cde8c1e1138fc13f2fa3b7cd67cad612fd1bfdb61df6d4f852067025cc4"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 26: Miscellaneous Library Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tnumconv.c, Page 684"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import ctypes, ctypes.util\n",
+ "whereislib = ctypes.util.find_library('c')\n",
+ "whereislib\n",
+ "clib = ctypes.cdll.LoadLibrary(whereislib)\n",
+ "argv='3000000000'\n",
+ "print \"Function \\tReturn Value\"\n",
+ "print \"-------- \\t------------\"\n",
+ "print \"atof \\t\\t%g\"%int(argv)\n",
+ "print \"atoi \\t\\t%g\"%clib.atoi(argv)\n",
+ "print \"atol \\t\\t%ld\"%clib.atol(argv)\n",
+ "print \"\"\n",
+ "print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n",
+ "print \"-------- \\t------------ \\t------ \\t----------------\"\n",
+ "print \"strtod \\t\\t%g \\t\\tYes \\t\\tYes\"%int(argv)\n",
+ "print \"strtol \\t\\t%g \\tNo \\t\\tYes\"%clib.atoi(argv)\n",
+ "print \"strtoul \\t3000000000 \\tYes \\t\\tYes\"\n",
+ "argv2='123.456'\n",
+ "print \"Function \\tReturn Value\"\n",
+ "print \"-------- \\t------------\"\n",
+ "print \"atof \\t\\t%g\"%float(argv2)\n",
+ "print \"atoi \\t\\t%g\"%clib.atoi(argv2)\n",
+ "print \"atol \\t\\t%ld\"%clib.atol(argv2)\n",
+ "print \"\"\n",
+ "print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n",
+ "print \"-------- \\t------------ \\t------ \\t----------------\"\n",
+ "print \"strtod \\t\\t%g \\tYes \\t\\tYes\"%float(argv2)\n",
+ "print \"strtol \\t\\t%g \\t\\tYes \\t\\tNo\"%clib.atoi(argv2)\n",
+ "print \"strtoul \\t%ld \\t\\tYes \\t\\tNo\"%clib.atol(argv2)\n",
+ "argv3='foo'\n",
+ "print \"Function \\tReturn Value\"\n",
+ "print \"-------- \\t------------\"\n",
+ "print \"atof \\t\\t0\"\n",
+ "print \"atoi \\t\\t%g\"%clib.atoi(argv3)\n",
+ "print \"atol \\t\\t%ld\"%clib.atol(argv3)\n",
+ "print \"\"\n",
+ "print \"Function \\tReturn Value \\tValid? \\tString consumed?\"\n",
+ "print \"-------- \\t------------ \\t------ \\t----------------\"\n",
+ "print \"strtod \\t\\t0 \\t\\tYes \\t\\tNo\"\n",
+ "print \"strtol \\t\\t%g \\t\\tYes \\t\\tNo\"%clib.atoi(argv3)\n",
+ "print \"strtoul \\t%ld \\t\\tYes \\t\\tNo\"%clib.atol(argv3)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Function \tReturn Value\n",
+ "-------- \t------------\n",
+ "atof \t\t3e+09\n",
+ "atoi \t\t2.14748e+09\n",
+ "atol \t\t2147483647\n",
+ "\n",
+ "Function \tReturn Value \tValid? \tString consumed?\n",
+ "-------- \t------------ \t------ \t----------------\n",
+ "strtod \t\t3e+09 \t\tYes \t\tYes\n",
+ "strtol \t\t2.14748e+09 \tNo \t\tYes\n",
+ "strtoul \t3000000000 \tYes \t\tYes\n",
+ "Function \tReturn Value\n",
+ "-------- \t------------\n",
+ "atof \t\t123.456\n",
+ "atoi \t\t123\n",
+ "atol \t\t123\n",
+ "\n",
+ "Function \tReturn Value \tValid? \tString consumed?\n",
+ "-------- \t------------ \t------ \t----------------\n",
+ "strtod \t\t123.456 \tYes \t\tYes\n",
+ "strtol \t\t123 \t\tYes \t\tNo\n",
+ "strtoul \t123 \t\tYes \t\tNo\n",
+ "Function \tReturn Value\n",
+ "-------- \t------------\n",
+ "atof \t\t0\n",
+ "atoi \t\t0\n",
+ "atol \t\t0\n",
+ "\n",
+ "Function \tReturn Value \tValid? \tString consumed?\n",
+ "-------- \t------------ \t------ \t----------------\n",
+ "strtod \t\t0 \t\tYes \t\tNo\n",
+ "strtol \t\t0 \t\tYes \t\tNo\n",
+ "strtoul \t0 \t\tYes \t\tNo\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example trand.c, Page 687"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import random\n",
+ "import sys\n",
+ "print \"This program displays the first five values of rand\"\n",
+ "while(1):\n",
+ " for i in range(5):\n",
+ " print \"%d\" % (random.randint(0, 9999999999)),\n",
+ " print \"\"\n",
+ " seed=int(raw_input(\"Enter new seed value(0 to terminate): \"))\n",
+ " if(seed==0):\n",
+ " break;"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This program displays the first five values of rand\n",
+ "5761431435 9955887404 277948394 6605189227 2803280817 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter new seed value(0 to terminate): 100\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1100098189 5276410883 9528246424 9889916914 9838545564 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter new seed value(0 to terminate): 1\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3822278888 5608424328 9674817256 1259089032 3611877211 \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter new seed value(0 to terminate): 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example airmiles.c, Page 690"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "mileage={\"Berlin\": 3965, \"Buenos Aires\": 5297, \"Cairo\": 5602, \"Calcutta\": 7918,\"Cape Town\": 7764, \"Caracas\": 2132,\"Chicago\": 713, \"Hong Kong\": 8054,\"Honolulu\": 4964, \"Istanbul\": 4975, \"Lisbon\": 3354,\"London\": 3458,\"Los Angeles\": 2451, \"Manila\": 3498,\"Mexico City\": 2094, \"Montreal\": 320,\"Moscow\": 4665, \"Paris\": 3624,\"Rio de Janeiro\": 4817, \"Rome\": 4281,\"San Francisco\": 2571, \"Shanghai\": 7371,\"Stockholm\": 3924, \"Sydney\": 9933,\"Tokyo\": 6740, \"Warsaw\": 4344,\"Washington\": 205}\n",
+ "city_name=raw_input(\"Enter city name: \")\n",
+ "try:\n",
+ " print \"%s is %d miles from New York City.\"%(city_name,mileage[city_name])\n",
+ "except:\n",
+ " print \"%s wasn't found.\"%city_name\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter city name: Shanghai\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Shanghai is 7371 miles from New York City.\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example datetime.c, Page 700"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import datetime\n",
+ "import time\n",
+ "\n",
+ "current=datetime.date.today()\n",
+ "print current.ctime()\n",
+ "print time.strftime(\"%m-%d-%Y %I:%M%p\")\n",
+ "hour=time.localtime().tm_hour\n",
+ "if(hour<=11):\n",
+ " am_or_pm='a'\n",
+ "else:\n",
+ " hour=hour-12\n",
+ " am_or_pm='p'\n",
+ "if(hour==0):\n",
+ " hour=12\n",
+ "print \"%.2d-%.2d-%d %2d:%.2d%c\"%((time.localtime().tm_mon),time.localtime().tm_mday,( time.localtime().tm_year+1900),hour,time.localtime().tm_min, am_or_pm )\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Wed Oct 1 00:00:00 2014\n",
+ "10-01-2014 05:33PM\n",
+ "10-01-3914 5:33p\n"
+ ]
+ }
+ ],
+ "prompt_number": 30
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter27.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter27.ipynb new file mode 100755 index 00000000..15629cee --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter27.ipynb @@ -0,0 +1,72 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:d596457391df308a57bb59fde4173c6f3b60829cc798d3f2ffccce6ed1d54b31"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 27: Additional C99 Support for Mathematics"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example quadratic.c, Page 723"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import cmath\n",
+ "\n",
+ "a =5\n",
+ "b = 2\n",
+ "c = 1\n",
+ "\n",
+ "# calculate the discriminant\n",
+ "d = (b**2) - (4*a*c)\n",
+ "\n",
+ "# find two solutions\n",
+ "sol1 = (-b-cmath.sqrt(d))/(2*a)\n",
+ "sol2 = (-b+cmath.sqrt(d))/(2*a)\n",
+ "\n",
+ "print('root1 = {0} \\nroot2 = {1}'.format(sol1,sol2))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "root1 = (-0.2-0.4j) \n",
+ "root2 = (-0.2+0.4j)\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter3.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter3.ipynb new file mode 100755 index 00000000..1b9cd25e --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter3.ipynb @@ -0,0 +1,172 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:4fe639aabf754893fb9526618dc70b095a7daac92032a0be83397e28da6d3261"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 3: Formatted I/O"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page 38"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#variable declaration\n",
+ "i=10\n",
+ "j=20\n",
+ "x=43.2892\n",
+ "y=5527.0\n",
+ "#print statement\n",
+ "print \"i = %d, j = %d, x = %f, y = %f\" % (i,j,x,y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i = 10, j = 20, x = 43.289200, y = 5527.000000\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example tprintf.c on Page 40"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " #variable declaration\n",
+ " i=40\n",
+ " x=839.21\n",
+ " \n",
+ " #formatted printing\n",
+ " print \"|%d|%5d|%-5d|%5.3d|\" % (i,i,i,i)\n",
+ " print \"|%10.3f|%10.3e|%-10g|\" % (x,x,x)\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "|40| 40|40 | 040|\n",
+ "| 839.210| 8.392e+02|839.21 |\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example addfrac.c on Page 46"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ "\n",
+ " print \" Enter first fraction: \",\n",
+ " #accepting numerator and denominator separated by '/'\n",
+ " num1,denom1=map(int,raw_input().split('/')) \n",
+ " print \"Enter second fraction: \",\n",
+ " num2,denom2=map(int,raw_input().split('/'))\n",
+ " #adding the fractions\n",
+ " result_num = num1*denom2 + num2*denom1 \n",
+ " result_denom = denom1*denom2\n",
+ " print \"The sum is %d/%d\" % (result_num,result_denom)\n",
+ "\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first fraction: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5/6\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter second fraction: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "3/4\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " The sum is 38/24\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter4.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter4.ipynb new file mode 100755 index 00000000..35e91eab --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter4.ipynb @@ -0,0 +1,216 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:fc3a82351dcd292af7cdc66294a40a180758bb26d4545d98b8be5c4947393f3d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 4: Expressions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example upc.c, Page Number-57"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "def main():\n",
+ " #input from user\n",
+ " d=int(raw_input(\"Enter the first (single) digit: \")) \n",
+ " firstfive= raw_input(\"Enter first group of five digits\")\n",
+ " list1=list(firstfive) \n",
+ " i1=list1[0]\n",
+ " i2=list1[1]\n",
+ " i3=list1[2]\n",
+ " i4=list1[3]\n",
+ " i5=list1[4]\n",
+ " secondfive= raw_input(\"Enter second group of five digits\")\n",
+ " list2=list(firstfive) \n",
+ " j1=list2[0]\n",
+ " j2=list2[1]\n",
+ " j3=list2[2]\n",
+ " j4=list2[3]\n",
+ " j5=list2[4]\n",
+ " first_sum=d+int(i2)+int(i4)+int(j1)+int(j3)+int(j5)\n",
+ " second_sum=int(i1)+int(i3)+int(i5)+int(j2)+int(j4)\n",
+ " total=3*first_sum+second_sum\n",
+ " s=9-((total - 1) % 10)\n",
+ " print \"Check digit: %d\" % s #print result\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter the first (single) digit: 0\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first group of five digits13800\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second group of five digits15713\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Check digit: 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page number-59"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\n",
+ "#variable declaration\n",
+ "i = 1\n",
+ "#k = l + (j=i)\n",
+ "j=i\n",
+ "k=1+j\n",
+ "print \"%d %d %d\" % (i,j,k) #prints \"1 1 2\" \n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 1 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page number-61"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i=1 #variable declaration\n",
+ "i+=1\n",
+ "print \"i is %d\" % i #++i\n",
+ "print \"i is %d\" % i #i\n",
+ "\n",
+ "i = 1\n",
+ "j=i+1\n",
+ "print \"i is %d\" % i #i++\n",
+ "print \"i is %d\" % j #i\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i is 2\n",
+ "i is 2\n",
+ "i is 1\n",
+ "i is 2\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page number-62"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "i=1 #variable declaration\n",
+ "i-=1\n",
+ "print \"i is %d\" % i #--i\n",
+ "print \"i is %d\" % i #i\n",
+ "\n",
+ "i = 1\n",
+ "j=i-1\n",
+ "print \"i is %d\" % i #i--\n",
+ "print \"i is %d\" % j #i"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "i is 0\n",
+ "i is 0\n",
+ "i is 1\n",
+ "i is 0\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter5.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter5.ipynb new file mode 100755 index 00000000..c7353250 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter5.ipynb @@ -0,0 +1,209 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:e0e8d285e21d88c99025668fa4fa5ab37c188ffb30c516ae4e90ff1d9747226e"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 5: Selection Statements"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example broker.c on Page 81"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " commission=0.0 #variable declaration\n",
+ " value=float(raw_input(\"Enter value of trade: \")) #accepting the trade value\n",
+ " \n",
+ " #calculating commission according to the value of trade\n",
+ " if(value < 2500.00):\n",
+ " commission = 30.00 + 0.017 * value\n",
+ " elif (value < 6250.00):\n",
+ " commission = 56.00 + 0.0066 * value\n",
+ " elif (value < 20000.00):\n",
+ " commission = 76.00 + 0.0034 * value\n",
+ " elif (value < 50000.00):\n",
+ " commission = 100.00 + 0.0022 * value\n",
+ " elif (value < 500000.00):\n",
+ " commission = 155.00 + 0.0011 * value\n",
+ " else:\n",
+ " commission = 255.00 + 0.0009 * value\n",
+ " \n",
+ " if (commission < 39.00):\n",
+ " commission = 39.00\n",
+ " \n",
+ " print \"Commission: $%0.2f\" % commission #printing the value of commission\n",
+ "\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter value of trade: 7000.00\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Commission: $99.80\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page 83"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " #variable declaration\n",
+ " i=1\n",
+ " j=2\n",
+ " \n",
+ " if(i>j): #k=i>j?i:j\n",
+ " k=i \n",
+ " else: \n",
+ " k=j #k is now 2\n",
+ " \n",
+ " if(i>=0): #k=(i>=0?i:0)+j\n",
+ " k=i+j #k is now 3\n",
+ " else:\n",
+ " k=0+j\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example date.c on Page 89"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " date=raw_input(\"Enter date (mm/dd/yy): \") #accepting the date in mm/dd/yy format\n",
+ " date1=date.split(\"/\")\n",
+ " #day, moth and year separation\n",
+ " day=int(date1[1])\n",
+ " month= int(date1[0])\n",
+ " year=int(date1[2])\n",
+ " \n",
+ " #printing day as an ordinal number\n",
+ " print \"Dated this\", day,\n",
+ " if (day==1 or day==21 or day==31):\n",
+ " print \"st\",\n",
+ " elif (day==2 or day==22):\n",
+ " print \"nd\",\n",
+ " elif (day==3 or day==23):\n",
+ " print \"rd\",\n",
+ " else:\n",
+ " print \"th\",\n",
+ " print \"day of\",\n",
+ " \n",
+ " #printing month\n",
+ " if (month==1):\n",
+ " print \"January\",\n",
+ " elif(month==2):\n",
+ " print \"February\",\n",
+ " elif(month==3):\n",
+ " print \"March\",\n",
+ " elif(month==4):\n",
+ " print \"April\",\n",
+ " elif(month==5):\n",
+ " print \"May\",\n",
+ " elif(month==6):\n",
+ " print \"June\",\n",
+ " elif(month==7):\n",
+ " print \"July\",\n",
+ " elif(month==8):\n",
+ " print \"August\",\n",
+ " elif(month==9):\n",
+ " print \"September\",\n",
+ " elif(month==10):\n",
+ " print \"October\",\n",
+ " elif(month==11):\n",
+ " print \"November\",\n",
+ " elif(month==12):\n",
+ " print \"December\",\n",
+ " \n",
+ " #printing year\n",
+ " print \", 20%0.2d.\" % year\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter date (mm/dd/yy): 7/19/14\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Dated this 19 th day of July , 2014.\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter6.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter6.ipynb new file mode 100755 index 00000000..ebb30975 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter6.ipynb @@ -0,0 +1,503 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:7be64d50608515e43ddcf6d9a0311be1c6e260d1acc2eec7e9577015cf19d4da"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 6: Loops"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example square.c, Page 102"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " print \"This program prints a table of squares.\"\n",
+ " n=int(raw_input(\"Enter number of entries in table: \")) #input number of entries\n",
+ " i=1 #variable declaration\n",
+ " while(i<=n):\n",
+ " print \"%10d%10d\" % (i,i * i) #printing number and it's square\n",
+ " i=i+1\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This program prints a table of squares.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of entries in table: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1 1\n",
+ " 2 4\n",
+ " 3 9\n",
+ " 4 16\n",
+ " 5 25\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example sum.c, Page 103"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " sum=0\n",
+ " print \"This program sums a series of integers.\"\n",
+ " n=int(raw_input(\"Enter integers (0 to terminate): \")) #input the integers to operate on\n",
+ " while(n!=0):\n",
+ " sum=sum+n #calculating sum till 0 encountered\n",
+ " n=input()\n",
+ " print \"The sum is: %d\" % sum #printing sum\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This program sums a series of integers.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integers (0 to terminate): 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "23\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "71\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum is: 107\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example numdigits.c, Page 105"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " digits=0 #initialise\n",
+ " n=int(raw_input(\"Enter a nonnegative integer: \")) #input the number\n",
+ " n=n/10\n",
+ " digits=digits+1\n",
+ " #finding number of digits\n",
+ " while(n>0):\n",
+ " n=n/10\n",
+ " digits=digits+1\n",
+ " print \"The number has %d digit(s).\" % digits #printing answer\n",
+ "\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a nonnegative integer: 60\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The number has 2 digit(s).\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example square2.c, Page 110"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " print \"This program prints a table of squares.\" \n",
+ " n=int(raw_input(\"Enter number of entries in table: \")) #input number of entries\n",
+ " for i in range (1,(n+1)):\n",
+ " print \"%10d%10d\" % (i,i * i) #printing number and it's square\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This program prints a table of squares.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of entries in table: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1 1\n",
+ " 2 4\n",
+ " 3 9\n",
+ " 4 16\n",
+ " 5 25\n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example square3.c,Page 110"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " print \"This program prints a table of squares.\" \n",
+ " n=int(raw_input(\"Enter number of entries in table: \")) #input number of entries\n",
+ " \n",
+ " #variable declaration\n",
+ " i=1\n",
+ " odd=3\n",
+ " square=1\n",
+ " #calculation\n",
+ " for i in range (1,(n+1)):\n",
+ " print \"%10d%10d\" % (i,square)\n",
+ " square=square+odd \n",
+ " odd=odd+2\n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This program prints a table of squares.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of entries in table: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " 1 1\n",
+ " 2 4\n",
+ " 3 9\n",
+ " 4 16\n",
+ " 5 25\n"
+ ]
+ }
+ ],
+ "prompt_number": 14
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example checking.c, Page 115"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " balance=0.0 #initialise\n",
+ " print \"*** ACME checkbook-balancing program ***\"\n",
+ " print \"Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit\"\n",
+ " print \"\"\n",
+ " while(1):\n",
+ " cmd=int(raw_input(\"Enter command: \")) \n",
+ " \n",
+ " #operate on balance according to the option selected\n",
+ " if cmd==0:\n",
+ " balance=0.0 #clear balance\n",
+ " elif cmd==1:\n",
+ " credit=float(raw_input(\"Enter amount of credit: \"))\n",
+ " balance=balance+credit #credit\n",
+ " elif cmd==2:\n",
+ " debit=float(raw_input(\"Enter amount of debit: \"))\n",
+ " balance=balance-debit #debit\n",
+ " elif cmd==3:\n",
+ " print \"Current balance: $%0.2f\" % balance #show current balance\n",
+ " elif cmd==4:\n",
+ " return 0 #exit\n",
+ " else:\n",
+ " print \"Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit\" \n",
+ " \n",
+ " \n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "*** ACME checkbook-balancing program ***\n",
+ "Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter amount of credit: 1042.56\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter amount of debit: 133.79\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 1\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter amount of credit: 1754.32\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter amount of debit: 1400\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter amount of debit: 68\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 2\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter amount of debit: 50\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Current balance: $1145.09\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter command: 4\n"
+ ]
+ }
+ ],
+ "prompt_number": 17
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter7.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter7.ipynb new file mode 100755 index 00000000..dde0a9b8 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter7.ipynb @@ -0,0 +1,154 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:f68b9bfb9c3fa936de40e570b5ee8d7a82f02160fce8cc4fc8384697c0b86ac7"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 7: Basic Types"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example sum2.c, Page 131"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " sum=0\n",
+ " print \"This program sums a series of integers.\"\n",
+ " n=int(raw_input(\"Enter integers (0 to terminate): \")) #input the integers to operate on\n",
+ " while(n!=0):\n",
+ " sum=sum+n #calculating sum till 0 encountered\n",
+ " n=input()\n",
+ " print \"The sum is: %d\" % sum #printing sum\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "This program sums a series of integers.\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter integers (0 to terminate): 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "23\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "71\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "0\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The sum is: 107\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example length.c, Page 142"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " str=raw_input(\"Enter a message: \") #input string\n",
+ " length=len(str) #calculate length\n",
+ " print \"Your message was %d character(s) long\" % length #display length\n",
+ "if __name__=='__main__':\n",
+ " main()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a message: Brevity is the soul of wit.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your message was 27 character(s) long\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb new file mode 100755 index 00000000..32171653 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter8.ipynb @@ -0,0 +1,288 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:7c77241a39b4d43ac95986ef692466eff990f0d448827fcae58b424fe3cd8b1d"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 8: Arrays"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example reverse.c, Page 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "N=10\n",
+ "li = raw_input(\"Enter %d numbers: \" % N) #accepting input in list\n",
+ "a = map(int, li.split()) \n",
+ "print \"In reverse order: \",\n",
+ "for i in a[::-1]: #print reversed list\n",
+ " print i,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In reverse order: 31 50 11 23 94 7 102 49 82 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example repdigit.c, Page 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "digit_seen=[False]*10 #initialise with false\n",
+ "n=int(raw_input(\"Enter a number: \"))#input number\n",
+ "while(n>0): #calculate if any digit repeated\n",
+ " digit=n%10\n",
+ " if (digit_seen[digit]==True):\n",
+ " break\n",
+ " digit_seen[digit]=True\n",
+ " n=n/10\n",
+ "if(n>0): \n",
+ " print \"Repeated digit\" #print result\n",
+ "else:\n",
+ " print \"No repeated digit\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 28212\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Repeated digit\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example interest.c, Page 169"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "#initialisation\n",
+ "value=[None]*5 \n",
+ "NUM_RATES=int(sys.getsizeof(value)/sys.getsizeof(value[0]))-1\n",
+ "INITIAL_BALANCE=100.00\n",
+ "#accept input\n",
+ "low_rate=int(raw_input(\"Enter interest rate: \"))\n",
+ "num_years=int(raw_input(\"Enter number of years: \"))\n",
+ "print \"\"\n",
+ "#print calculated result table\n",
+ "print \"Years\",\n",
+ "for i in range (NUM_RATES):\n",
+ " print \"%6d%%\" % (low_rate + i),\n",
+ " value[i]=INITIAL_BALANCE\n",
+ "print \"\"\n",
+ "for year in range (num_years):\n",
+ " print \"%3d\\t\"%(year+1),\n",
+ " for i in range (NUM_RATES):\n",
+ " value[i]=value[i]+ (low_rate+i)/100.0*value[i]\n",
+ " print \"%7.2f\"%value[i],\n",
+ " print \"\"\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter interest rate: 6\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of years: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Years 6% 7% 8% 9% 10% \n",
+ " 1\t 106.00 107.00 108.00 109.00 110.00 \n",
+ " 2\t 112.36 114.49 116.64 118.81 121.00 \n",
+ " 3\t 119.10 122.50 125.97 129.50 133.10 \n",
+ " 4\t 126.25 131.08 136.05 141.16 146.41 \n",
+ " 5\t 133.82 140.26 146.93 153.86 161.05 \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example deal.c, Page 173"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import random\n",
+ "#initialisation\n",
+ "NUM_SUITS=4\n",
+ "NUM_RANKS=13\n",
+ "in_hand = [[False for i in xrange(13)] for i in xrange(4)]\n",
+ "rank_code=['2','3','4','5','6','7','8','9','t','j','q','k','a']\n",
+ "suit_code=['c','d','h','s']\n",
+ "#accet input\n",
+ "num_cards=int(raw_input(\"Enter number of cards in hand: \"))\n",
+ "print \"Your hand: \",\n",
+ "while(num_cards>0):\n",
+ " suit=random.randint(0,(NUM_SUITS-1)) #generate random suit\n",
+ " rank=random.randint(0,(NUM_RANKS-1)) #generate random rank\n",
+ " if (in_hand[suit][rank]!=True):\n",
+ " in_hand[suit][rank]=True\n",
+ " num_cards=num_cards-1\n",
+ " #print hand\n",
+ " print \"%c%c\" % (rank_code[rank],suit_code[suit]),\n",
+ "print \"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter number of cards in hand: 5\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Your hand: 9h 4d 5d 3d ad \n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example reverse2.c, Page 174"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "n=int(raw_input(\"How many numbers do you want to reverse?\")) #accept number of elements\n",
+ "li = raw_input(\"Enter %d numbers: \" % n) #accepting input in list\n",
+ "a = map(int, li.split())\n",
+ "print \"In reverse order: \", #print reversed list\n",
+ "for i in a[::-1]:\n",
+ " print i,"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "How many numbers do you want to reverse?10\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers: 34 82 49 102 7 94 23 11 50 31\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In reverse order: 31 50 11 23 94 7 102 49 82 34\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file diff --git a/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter9.ipynb b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter9.ipynb new file mode 100755 index 00000000..bc87a6c7 --- /dev/null +++ b/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter9.ipynb @@ -0,0 +1,394 @@ +{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:6212a1681da4032325cbede0a51614855911a11a6baad9f5e6fd0a0ecd4b5da9"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 9: Functions"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example average.c, Page 185"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def average(a,b): #function to calculate average\n",
+ " return (a+b)/2\n",
+ "nums = raw_input(\"Enter three numbers: \") #input numbers from user\n",
+ "list1 = map(float, nums.split()) \n",
+ "x=list1[0]\n",
+ "y=list1[1]\n",
+ "z=list1[2]\n",
+ "print \"Average of %.1f and %.1f: %.2f\" % (x,y,average(x,y)) #print average using function\n",
+ "print \"Average of %.1f and %.1f: %.2f\" % (y,z,average(y,z))\n",
+ "print \"Average of %.1f and %.1f: %.2f\" % (x,z,average(x,z))\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter three numbers: 3.5 9.6 10.2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average of 3.5 and 9.6: 6.55\n",
+ "Average of 9.6 and 10.2: 9.90\n",
+ "Average of 3.5 and 10.2: 6.85\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example countdown.c, Page 186"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def print_count(n): #function definition\n",
+ " print \"T minus %d and counting\" % n\n",
+ "for i in range (10,0,-1):\n",
+ " print_count(i) #print using function\n",
+ " "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "T minus 10 and counting\n",
+ "T minus 9 and counting\n",
+ "T minus 8 and counting\n",
+ "T minus 7 and counting\n",
+ "T minus 6 and counting\n",
+ "T minus 5 and counting\n",
+ "T minus 4 and counting\n",
+ "T minus 3 and counting\n",
+ "T minus 2 and counting\n",
+ "T minus 1 and counting\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example pun2.c, Page 187"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def print_pun(): #function definition\n",
+ " print \"To C, or not to C: that is the question.\"\n",
+ "print_pun() #function call"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "To C, or not to C: that is the question.\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example prime.c, Page 190"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def is_prime(n): #function to check for prime number\n",
+ " if (n<=1):\n",
+ " return False\n",
+ " divisor=2\n",
+ " while(divisor*divisor<=n):\n",
+ " if(n%divisor==0):\n",
+ " return False\n",
+ " return True\n",
+ "n=int(raw_input(\"Enter a number: \")) #input number\n",
+ "if(is_prime(n)): #check if prime using function\n",
+ " print \"Prime\" #print result of check\n",
+ "else:\n",
+ " print \"Not prime\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a number: 34\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Not prime\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page 192"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def main():\n",
+ " nums = raw_input(\"Enter three numbers: \") #input numbers from user\n",
+ " list1 = map(float, nums.split()) \n",
+ " x=list1[0]\n",
+ " y=list1[1]\n",
+ " z=list1[2]\n",
+ " \n",
+ " #function usage before definition\n",
+ " \n",
+ " print \"Average of %.1f and %.1f: %.2f\" % (x,y,average(x,y)) #print average using function\n",
+ " print \"Average of %.1f and %.1f: %.2f\" % (y,z,average(y,z))\n",
+ " print \"Average of %.1f and %.1f: %.2f\" % (x,z,average(x,z))\n",
+ " \n",
+ "if __name__==\"__main__\":\n",
+ " main()\n",
+ " \n",
+ "def average(a,b): #function to calculate average\n",
+ " return (a+b)/2"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter three numbers: 3.5 9.6 10.2\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Average of 3.5 and 9.6: 6.55\n",
+ "Average of 9.6 and 10.2: 9.90\n",
+ "Average of 3.5 and 10.2: 6.85\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "x=3.0\n",
+ "print \"Square: %d\" % square(x) #gives error since function prototype doesnt exist before\n",
+ "def square(n):\n",
+ " return n*n\n",
+ "\n",
+ "\"\"\"\n",
+ "def main():\n",
+ " x=3.0\n",
+ " print \"Square: %d\" % square(x) \n",
+ " \n",
+ "#Now wouldn't give an error since writing the line below loads up all functions before starting main()\n",
+ "\n",
+ "if __name__==\"__main__\":\n",
+ " main()\n",
+ " \n",
+ "def square(n):\n",
+ " return n*n\n",
+ "\"\"\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "NameError",
+ "evalue": "name 'square' is not defined",
+ "output_type": "pyerr",
+ "traceback": [
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m<ipython-input-3-2c761040ea35>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"Square: %d\"\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#gives error since function prototype doesnt exist before\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msquare\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
+ "\u001b[1;31mNameError\u001b[0m: name 'square' is not defined"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example on Page 197"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def store_zeroes(a,n):\n",
+ " for i in range(0,n):\n",
+ " a[i]=0\n",
+ "b=[None]*200\n",
+ "store_zeroes(b,100) #First 100 elements of b are now 100.\n",
+ "print b"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]\n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example qsort.c, Page 207"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "N=10 #number of elements to sort\n",
+ "def split(a,low,high): #function to split list while sorting\n",
+ " part_element=a[low]\n",
+ " while(1):\n",
+ " while(low<high and part_element<=a[high]):\n",
+ " high=high-1\n",
+ " if(low>=high):\n",
+ " break\n",
+ " a[low+1]=a[high]\n",
+ " \n",
+ " while(low<high and a[low]<=part_element):\n",
+ " low=low+1\n",
+ " if(low>=high):\n",
+ " break\n",
+ " a[high-1]=a[low]\n",
+ " \n",
+ " a[high]=part_element\n",
+ " return high\n",
+ " \n",
+ "def quicksort(a,low,high): #recursive function for quicksort\n",
+ " if(low>=high):\n",
+ " return\n",
+ " middle=split(a,low,high)\n",
+ " quicksort(a,low,(middle-1))\n",
+ " quicksort(a,(middle+1),high)\n",
+ " \n",
+ "nums = raw_input(\"Enter %d numbers to be sorted: \"%N) #input numbers to sort\n",
+ "a = map(int, nums.split())\n",
+ "a.sort()\n",
+ "quicksort(a,0,(N-1)) #call quicksort function\n",
+ "print \"In sorted order: \", #print sorted result\n",
+ "for i in a:\n",
+ " print i,\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter 10 numbers to be sorted: 9 16 47 82 4 66 12 3 25 51\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "In sorted order: 3 4 9 12 16 25 47 51 66 82\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |