"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"\n",
"def play():\n",
" print \"Throwing the dice....\"\n",
" score1=throw()\n",
" print \"%2d\" %(score1)\n",
"\n",
" if score1==7 or score1==11:\n",
" print \"Congratulations!! you WIN on the first throw\"\n",
"\n",
" elif score1==2 or score1==3 or score1==12:\n",
" print \"sorry!! you LOSE on the first throw\"\n",
"\n",
" else:\n",
" while(True):\n",
" print \"Throwing the dice again...\"\n",
" score2=throw()\n",
" print \"%2d\" %(score2)\n",
" if score2==score1 or score2==7:\n",
" break\n",
"\n",
" if score2==score1:\n",
" print \"You WIN by matching your first score\"\n",
" else:\n",
" print \"You LOSE by failing to match your first score\"\n",
"\n",
"\n",
" return\n",
"\n",
"\n",
"def throw():\n",
"\n",
" n1=random.randrange(1,7)\n",
" n2=random.randrange(1,7)\n",
" return n1+n2\n",
"\n",
"\n",
"import random\n",
"\n",
"\n",
"print \"Welcome to the Game of Craps \\n\\n\"\n",
"random.seed(2365)\n",
"play()\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Welcome to the Game of Craps \n",
"\n",
"\n",
"Throwing the dice....\n",
" 9\n",
"Throwing the dice again...\n",
" 4\n",
"Throwing the dice again...\n",
"11\n",
"Throwing the dice again...\n",
" 9\n",
"You WIN by matching your first score\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.12, Page number: 7.18
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def modify(a):\n",
" a=a*3\n",
" print \"a= %d (from the function, after being modified)\" %(a)\n",
" return\n",
"\n",
"a=2\n",
"print \"a=%d (from main, before calling the function)\" %(a)\n",
"modify(a)\n",
"print \"a=%d (from main, after calling the function)\" %(a)\n",
"\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"a=2 (from main, before calling the function)\n",
"a= 6 (from the function, after being modified)\n",
"a=2 (from main, after calling the function)\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.13, Page number: 7.19
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def sl(val,n):\n",
" deprec=val/n\n",
" print \"in \",n\n",
" \n",
" for year in range(1,n+1):\n",
" val=val-deprec\n",
" writeoutput(year,deprec,val)\n",
"\n",
" return\n",
"\n",
"def ddb(val,n):\n",
" for year in range(1,n+1):\n",
" deprec=2*val/n\n",
" val=val-deprec\n",
" writeoutput(year,deprec,val)\n",
"\n",
" return\n",
"\n",
"def syd(val,n):\n",
" tag=val\n",
" for year in range(1,n+1):\n",
" deprec=(n-year+1)*tag/(n*(n+1)/2)\n",
" val=val-deprec\n",
" writeoutput(year,deprec,val)\n",
"\n",
" return\n",
"\n",
"def writeoutput(year,depreciation,value):\n",
" print \"End of the year %2d Depreciation: %7.2f Current Value: %8.2f\" %(year,depreciation,value)\n",
"\n",
" return\n",
"\n",
"\n",
"\n",
"def main(choice,val,n):\n",
" \n",
" print \"Original value : \",val\n",
" val=float(val)\n",
" print \"Number of years : \",n\n",
"\n",
" if choice==1:\n",
" print \"Straight-Line Method\\n\\n\"\n",
" sl(val,n)\n",
" elif choice==2:\n",
" print \"Double-Declining-Balance Method \\n\\n\"\n",
" ddb(val,n)\n",
" elif choice==3:\n",
" print \"Sum-Of-The-Years'-Digits Method\"\n",
" syd(val,n)\n",
"\n",
" return\n",
"\n",
"\n",
"print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
"main(1,8000,10)\n",
"print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
"main(2,8000,10)\n",
"print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
"main(3,8000,10)\n",
"print \"\\n\\nMethod: (1-SL 2-DDB 3-SYD)\"\n",
"main(1,5000,4)\n",
"\n",
"\n",
" \n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Method: (1-SL 2-DDB 3-SYD)\n",
"Original value : 8000\n",
"Number of years : 10\n",
"Straight-Line Method\n",
"\n",
"\n",
"in 10\n",
"End of the year 1 Depreciation: 800.00 Current Value: 7200.00\n",
"End of the year 2 Depreciation: 800.00 Current Value: 6400.00\n",
"End of the year 3 Depreciation: 800.00 Current Value: 5600.00\n",
"End of the year 4 Depreciation: 800.00 Current Value: 4800.00\n",
"End of the year 5 Depreciation: 800.00 Current Value: 4000.00\n",
"End of the year 6 Depreciation: 800.00 Current Value: 3200.00\n",
"End of the year 7 Depreciation: 800.00 Current Value: 2400.00\n",
"End of the year 8 Depreciation: 800.00 Current Value: 1600.00\n",
"End of the year 9 Depreciation: 800.00 Current Value: 800.00\n",
"End of the year 10 Depreciation: 800.00 Current Value: 0.00\n",
"\n",
"\n",
"Method: (1-SL 2-DDB 3-SYD)\n",
"Original value : 8000\n",
"Number of years : 10\n",
"Double-Declining-Balance Method \n",
"\n",
"\n",
"End of the year 1 Depreciation: 1600.00 Current Value: 6400.00\n",
"End of the year 2 Depreciation: 1280.00 Current Value: 5120.00\n",
"End of the year 3 Depreciation: 1024.00 Current Value: 4096.00\n",
"End of the year 4 Depreciation: 819.20 Current Value: 3276.80\n",
"End of the year 5 Depreciation: 655.36 Current Value: 2621.44\n",
"End of the year 6 Depreciation: 524.29 Current Value: 2097.15\n",
"End of the year 7 Depreciation: 419.43 Current Value: 1677.72\n",
"End of the year 8 Depreciation: 335.54 Current Value: 1342.18\n",
"End of the year 9 Depreciation: 268.44 Current Value: 1073.74\n",
"End of the year 10 Depreciation: 214.75 Current Value: 858.99\n",
"\n",
"\n",
"Method: (1-SL 2-DDB 3-SYD)\n",
"Original value : 8000\n",
"Number of years : 10\n",
"Sum-Of-The-Years'-Digits Method\n",
"End of the year 1 Depreciation: 1454.55 Current Value: 6545.45\n",
"End of the year 2 Depreciation: 1309.09 Current Value: 5236.36\n",
"End of the year 3 Depreciation: 1163.64 Current Value: 4072.73\n",
"End of the year 4 Depreciation: 1018.18 Current Value: 3054.55\n",
"End of the year 5 Depreciation: 872.73 Current Value: 2181.82\n",
"End of the year 6 Depreciation: 727.27 Current Value: 1454.55\n",
"End of the year 7 Depreciation: 581.82 Current Value: 872.73\n",
"End of the year 8 Depreciation: 436.36 Current Value: 436.36\n",
"End of the year 9 Depreciation: 290.91 Current Value: 145.45\n",
"End of the year 10 Depreciation: 145.45 Current Value: 0.00\n",
"\n",
"\n",
"Method: (1-SL 2-DDB 3-SYD)\n",
"Original value : 5000\n",
"Number of years : 4\n",
"Straight-Line Method\n",
"\n",
"\n",
"in 4\n",
"End of the year 1 Depreciation: 1250.00 Current Value: 3750.00\n",
"End of the year 2 Depreciation: 1250.00 Current Value: 2500.00\n",
"End of the year 3 Depreciation: 1250.00 Current Value: 1250.00\n",
"End of the year 4 Depreciation: 1250.00 Current Value: 0.00\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def reverse(text,n):\n",
"\n",
" if n<0:\n",
" return\n",
" else:\n",
" print text[n],\n",
" reverse(text,n-1)\n",
"\n",
"\n",
"\n",
"text='Now is the time for all good men to come to tje aid of their country!'\n",
"n=len(text)\n",
"reverse(text,n-1)\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"! y r t n u o c r i e h t f o d i a e j t o t e m o c o t n e m d o o g l l a r o f e m i t e h t s i w o N\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 7.16, Page number: 7.16
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def transfer(n,From,to,temp):\n",
"\n",
" if n>0:\n",
" transfer(n-1,From,temp,to)\n",
" print \"Move disk %d from %c to %c\" %(n,From,to)\n",
" transfer(n-1,temp,to,From)\n",
"\n",
" return\n",
"\n",
"print \"Welcome to the TOWERS OF HANOI \\n\\n\"\n",
"transfer(3,'L','R','C')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Welcome to the TOWERS OF HANOI \n",
"\n",
"\n",
"Move disk 1 from L to R\n",
"Move disk 2 from L to C\n",
"Move disk 1 from R to C\n",
"Move disk 3 from L to R\n",
"Move disk 1 from C to L\n",
"Move disk 2 from C to R\n",
"Move disk 1 from L to R\n"
]
}
],
"prompt_number": 17
}
],
"metadata": {}
}
]
}