summaryrefslogtreecommitdiff
path: root/Beginning_C_By_Ivon_Horton/chapter13.ipynb
diff options
context:
space:
mode:
authorJovina Dsouza2014-06-18 12:43:07 +0530
committerJovina Dsouza2014-06-18 12:43:07 +0530
commit206d0358703aa05d5d7315900fe1d054c2817ddc (patch)
treef2403e29f3aded0caf7a2434ea50dd507f6545e2 /Beginning_C_By_Ivon_Horton/chapter13.ipynb
parentc6f0d6aeb95beaf41e4b679e78bb42c4ffe45a40 (diff)
downloadPython-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.gz
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.tar.bz2
Python-Textbook-Companions-206d0358703aa05d5d7315900fe1d054c2817ddc.zip
adding book
Diffstat (limited to 'Beginning_C_By_Ivon_Horton/chapter13.ipynb')
-rw-r--r--Beginning_C_By_Ivon_Horton/chapter13.ipynb330
1 files changed, 330 insertions, 0 deletions
diff --git a/Beginning_C_By_Ivon_Horton/chapter13.ipynb b/Beginning_C_By_Ivon_Horton/chapter13.ipynb
new file mode 100644
index 00000000..8a2ffbe5
--- /dev/null
+++ b/Beginning_C_By_Ivon_Horton/chapter13.ipynb
@@ -0,0 +1,330 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 13: The Preprocessor and Debugging"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 13.2, page no. 576"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Demonstrating assertions\n",
+ "\"\"\"\n",
+ "\n",
+ "y = 5\n",
+ "for x in range(20):\n",
+ " print \"x = %d y = %d\" %(x, y)\n",
+ " assert(x < y)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "AssertionError",
+ "evalue": "",
+ "output_type": "pyerr",
+ "traceback": [
+ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m<ipython-input-1-3bb87684a82b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;34m\"x = %d y = %d\"\u001b[0m \u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m<\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
+ "\u001b[1;31mAssertionError\u001b[0m: "
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "x = 0 y = 5\n",
+ "x = 1 y = 5\n",
+ "x = 2 y = 5\n",
+ "x = 3 y = 5\n",
+ "x = 4 y = 5\n",
+ "x = 5 y = 5\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 13.3, page no. 578"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Test our timer function\n",
+ "\"\"\"\n",
+ "\n",
+ "import time\n",
+ "import datetime\n",
+ "import math\n",
+ "\n",
+ "calendar_start = datetime.datetime.now()\n",
+ "cpu_start = time.time()\n",
+ "count = 0\n",
+ "iterations = 1000000\n",
+ "answer = 'y'\n",
+ "x = 0.0\n",
+ "print \"Initial clock time = \", cpu_start, \" Initial calendar time = \", calendar_start\n",
+ "while(answer.lower() == 'y'):\n",
+ " for i in range(iterations):\n",
+ " x = math.sqrt(3.14159265)\n",
+ " count += 1\n",
+ " print \"%d square roots completed.\\n\" %(iterations*count)\n",
+ " print \"Do you want to run some more(y or n)? \"\n",
+ " answer = raw_input()\n",
+ "\n",
+ "cpu_end = time.time()\n",
+ "calendar_end = datetime.datetime.now()\n",
+ "\n",
+ "\n",
+ "print \"Final clock time = \", cpu_end, \" Final calendar time = \", calendar_end\n",
+ "print \"CPU time for %d iterations is %.2f seconds\" %(count*iterations, (float(cpu_end-cpu_start)/time.clock()))\n",
+ "print \"Elapsed calendar time to execute the program is\", calendar_end-calendar_start, \" seconds\""
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Initial clock time = 1387828819.12 Initial calendar time = 2013-12-24 01:30:19.115309\n",
+ "1000000 square roots completed.\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "Do you want to run some more(y or n)? \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "y\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "2000000 square roots completed.\n",
+ "\n",
+ "Do you want to run some more(y or n)? \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "n\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Final clock time = 1387828824.23 Final calendar time = 2013-12-24 01:30:24.232139\n",
+ "CPU time for 2000000 iterations is 4.49 seconds\n",
+ "Elapsed calendar time to execute the program is 0:00:05.116830 seconds\n"
+ ]
+ }
+ ],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 13.4, page no. 583"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Getting date data with ease\n",
+ "\"\"\"\n",
+ "\n",
+ "from datetime import *\n",
+ "import time\n",
+ "\n",
+ "day = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n",
+ "month = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\",\n",
+ " \"September\", \"October\", \"November\", \"December\"]\n",
+ " \n",
+ "now = datetime.now()\n",
+ "suffix = \"\"\n",
+ "\n",
+ "today = day[datetime.today().weekday()]\n",
+ "curr_month = month[now.month-1]\n",
+ "\n",
+ "if now.day == 1 or now.day == 21 or now.day == 31:\n",
+ " suffix= \"st\"\n",
+ "elif now.day == 2 or now.day == 22:\n",
+ " suffix= \"nd\"\n",
+ "elif now.day == 3 or now.day == 23:\n",
+ " suffix = \"rd\"\n",
+ "else:\n",
+ " suffix= \"th\"\n",
+ " \n",
+ "print \"Today is \", today, \" the \",str(now.day)+suffix, curr_month, str(now.year)+\".\", \"The time is \", time.strftime(\"%H:%M:%S\")"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Today is Tuesday the 24th December 2013. The time is 01:30:43\n"
+ ]
+ }
+ ],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Program 13.5, page no. 586"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "\"\"\"\n",
+ "Getting the day for a given date\n",
+ "\"\"\"\n",
+ "\n",
+ "from datetime import *\n",
+ "import time\n",
+ "\n",
+ "day = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"]\n",
+ "month = [\"January\", \"February\", \"March\", \"April\", \"May\", \"June\", \"July\", \"August\",\n",
+ " \"September\", \"October\", \"November\", \"December\"]\n",
+ " \n",
+ "now = datetime.now()\n",
+ "suffix = \"\"\n",
+ "\n",
+ "curr_month = month[now.month-1]\n",
+ "\n",
+ "print \"Enter a name: \",\n",
+ "name = raw_input()\n",
+ "print \"Enter a DOB for \", name\n",
+ "dob_day = int(raw_input(\"Day: \"))\n",
+ "dob_month = int(raw_input(\"Month: \"))\n",
+ "dob_year = int(raw_input(\"Year: \"))\n",
+ "dob_date = date(dob_year, dob_month, dob_day)\n",
+ "\n",
+ "if dob_day == 1 or dob_day == 21 or dob_day == 31:\n",
+ " suffix= \"st\"\n",
+ "elif dob_day == 2 or dob_day == 22:\n",
+ " suffix= \"nd\"\n",
+ "elif dob_day == 3 or dob_day == 23:\n",
+ " suffix = \"rd\"\n",
+ "else:\n",
+ " suffix= \"th\"\n",
+ "\n",
+ "dob_month = month[dob_month-1]\n",
+ "print name, \"Was born on \", str(dob_day)+suffix, dob_month, dob_year, \"which was a \", day[date.weekday(dob_date)]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter a name: "
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ricky\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " Enter a DOB for Ricky\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Day: 5\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Month: 8\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Year: 1970\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Ricky Was born on 5th August 1970 which was a Wednesday\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file