"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def linecount(text):\n",
"\t\n",
"\treturn len(text)\n",
"\n",
"Sum=0\n",
"lines=['Now is the time for all good men','to come to the aid of their country.']\n",
"for i in lines:\n",
"\tSum+=linecount(i)\n",
"\n",
"count=len(lines)\n",
"count=float(count)\n",
"avg=Sum/count\n",
"\n",
"print 'Average numbers of characters per line: ',avg\n",
"\t\n",
"\t"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Average numbers of characters per line: 34.0\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def linecount(text):\n",
"\t\n",
"\tglobal Sum\n",
"\tSum += len(text)\n",
"\n",
"Sum=0\n",
"lines=['Now is the time for all good men','to come to the aid of their country.']\n",
"for i in lines:\n",
"\tlinecount(i)\n",
"\n",
"count=len(lines)\n",
"count=float(count)\n",
"avg=Sum/count\n",
"\n",
"print 'Average numbers of characters per line: ',avg\n",
"\t\n",
"\t\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Average numbers of characters per line: 34.0\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 8.7, Page number: 8.12
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def fibonacci(count):\n",
"\t\n",
"\tif count<3:\n",
"\t\tf=1\n",
"\telse:\n",
"\t\tf = fibonacci.f1 + fibonacci.f2\n",
"\t\tfibonacci.f2 = fibonacci.f1\n",
"\t\tfibonacci.f1 = f\n",
"\t\t\n",
"\treturn f\n",
"\t\t\n",
"fibonacci.f1,fibonacci.f2=1,1\n",
"n=30\n",
"for count in range(1,n+1):\n",
"\t\n",
"\tprint 'i = %2d F = %d' %(count,fibonacci(count))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"i = 1 F = 1\n",
"i = 2 F = 1\n",
"i = 3 F = 2\n",
"i = 4 F = 3\n",
"i = 5 F = 5\n",
"i = 6 F = 8\n",
"i = 7 F = 13\n",
"i = 8 F = 21\n",
"i = 9 F = 34\n",
"i = 10 F = 55\n",
"i = 11 F = 89\n",
"i = 12 F = 144\n",
"i = 13 F = 233\n",
"i = 14 F = 377\n",
"i = 15 F = 610\n",
"i = 16 F = 987\n",
"i = 17 F = 1597\n",
"i = 18 F = 2584\n",
"i = 19 F = 4181\n",
"i = 20 F = 6765\n",
"i = 21 F = 10946\n",
"i = 22 F = 17711\n",
"i = 23 F = 28657\n",
"i = 24 F = 46368\n",
"i = 25 F = 75025\n",
"i = 26 F = 121393\n",
"i = 27 F = 196418\n",
"i = 28 F = 317811\n",
"i = 29 F = 514229\n",
"i = 30 F = 832040\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Example 8.9, Page number: 8.18
"
]
},
{
"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(563)\n",
"play()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Welcome to the Game of Craps \n",
"\n",
"\n",
"Throwing the dice....\n",
"10\n",
"Throwing the dice again...\n",
" 3\n",
"Throwing the dice again...\n",
"12\n",
"Throwing the dice again...\n",
" 9\n",
"Throwing the dice again...\n",
" 7\n",
"You LOSE by failing to match your first score\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def fibonacci(count):\n",
"\t\n",
"\tif count<3:\n",
"\t\tf=1\n",
"\telse:\n",
"\t\tf = fibonacci.f1 + fibonacci.f2\n",
"\t\tfibonacci.f2 = fibonacci.f1\n",
"\t\tfibonacci.f1 = f\n",
"\t\t\n",
"\treturn f\n",
"\t\t\n",
"fibonacci.f1,fibonacci.f2=1,1\n",
"n=40\n",
"for count in range(1,n+1):\n",
"\t\n",
"\tprint 'i = %2d F = %d' %(count,fibonacci(count))\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"i = 1 F = 1\n",
"i = 2 F = 1\n",
"i = 3 F = 2\n",
"i = 4 F = 3\n",
"i = 5 F = 5\n",
"i = 6 F = 8\n",
"i = 7 F = 13\n",
"i = 8 F = 21\n",
"i = 9 F = 34\n",
"i = 10 F = 55\n",
"i = 11 F = 89\n",
"i = 12 F = 144\n",
"i = 13 F = 233\n",
"i = 14 F = 377\n",
"i = 15 F = 610\n",
"i = 16 F = 987\n",
"i = 17 F = 1597\n",
"i = 18 F = 2584\n",
"i = 19 F = 4181\n",
"i = 20 F = 6765\n",
"i = 21 F = 10946\n",
"i = 22 F = 17711\n",
"i = 23 F = 28657\n",
"i = 24 F = 46368\n",
"i = 25 F = 75025\n",
"i = 26 F = 121393\n",
"i = 27 F = 196418\n",
"i = 28 F = 317811\n",
"i = 29 F = 514229\n",
"i = 30 F = 832040\n",
"i = 31 F = 1346269\n",
"i = 32 F = 2178309\n",
"i = 33 F = 3524578\n",
"i = 34 F = 5702887\n",
"i = 35 F = 9227465\n",
"i = 36 F = 14930352\n",
"i = 37 F = 24157817\n",
"i = 38 F = 39088169\n",
"i = 39 F = 63245986\n",
"i = 40 F = 102334155\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"