summaryrefslogtreecommitdiff
path: root/Magnifying_C/Chapter_10.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Magnifying_C/Chapter_10.ipynb')
-rwxr-xr-xMagnifying_C/Chapter_10.ipynb336
1 files changed, 336 insertions, 0 deletions
diff --git a/Magnifying_C/Chapter_10.ipynb b/Magnifying_C/Chapter_10.ipynb
new file mode 100755
index 00000000..1a4d1459
--- /dev/null
+++ b/Magnifying_C/Chapter_10.ipynb
@@ -0,0 +1,336 @@
+{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 10: Miscellany"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.1, Page number: 356"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "# Calculation and result\n",
+ "i = 0\n",
+ "for arg in sys.argv :\n",
+ " print ('arg [%d] - %s' % (i, arg))\n",
+ " i = i+1"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%run 10.1.py hello world"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "arg [0] - 10.1.py\n",
+ "arg [1] - hello\n",
+ "arg [2] - world\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.2, Page number: 358"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "if len(sys.argv) != 2 :\n",
+ "\tprint ('Inappropriate number of arguments')\n",
+ "\t\n",
+ "total = 0\n",
+ "def fact (n) :\n",
+ " total = n\n",
+ " while n > 1 :\n",
+ " total = total * (n - 1)\n",
+ " n = n - 1\n",
+ " return total\n",
+ "\n",
+ "print ('Factorial of %d is %d ' % (int(sys.argv[1]), fact (int(sys.argv[1]))))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%run 10.2.py 5"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Factorial of 5 is 120 \n"
+ ]
+ }
+ ],
+ "prompt_number": 7
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.3, Page number: 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import sys\n",
+ "\n",
+ "# Function declaration\n",
+ "def printdata () :\n",
+ "\ti = 0\n",
+ "\tfor arg in sys.argv[1:] :\n",
+ "\t\tprint ('The data in cell [%d] is %s ' % (i, arg))\n",
+ "\t\ti = i+1\n",
+ "\n",
+ "# Calculation and result\n",
+ "print ('The names before sorting are ')\n",
+ "printdata ()\n",
+ "\n",
+ "sys.argv.sort() \n",
+ "\n",
+ "print ('The names after sorting are ')\n",
+ "printdata ()"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%run 10.3.py zimbabwe nigeria argentina china hongkong"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "The names before sorting are \n",
+ "The data in cell [0] is zimbabwe \n",
+ "The data in cell [1] is nigeria \n",
+ "The data in cell [2] is argentina \n",
+ "The data in cell [3] is china \n",
+ "The data in cell [4] is hongkong \n",
+ "The names after sorting are \n",
+ "The data in cell [0] is argentina \n",
+ "The data in cell [1] is china \n",
+ "The data in cell [2] is hongkong \n",
+ "The data in cell [3] is nigeria \n",
+ "The data in cell [4] is zimbabwe \n"
+ ]
+ }
+ ],
+ "prompt_number": 8
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.4, Page number: 377"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter any positive number '))\n",
+ "\n",
+ "# Function declaration, calculation and result\n",
+ "def convert (a) :\n",
+ " return int(bin(a)[2:])\n",
+ "\n",
+ "bits = str(convert (a))\n",
+ "print ('Number of bits = %d' % (len(bits)))\n",
+ "print ('Binary equivalent is : %d' % (convert (a)))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter any positive number 16\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Number of bits = 5\n",
+ "Binary equivalent is : 10000\n"
+ ]
+ }
+ ],
+ "prompt_number": 13
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.5, Page number: 379"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number : '))\n",
+ "b = int(raw_input('Enter second number : '))\n",
+ "\n",
+ "# Calculation and result\n",
+ "b = ~b + 1\n",
+ "c = a + b\n",
+ "\n",
+ "print ('Subtraction is = %d ' % c)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number : 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Subtraction is = 4 \n"
+ ]
+ }
+ ],
+ "prompt_number": 11
+ },
+ {
+ "cell_type": "heading",
+ "level": 2,
+ "metadata": {},
+ "source": [
+ "Example 10.6, Page number: 380"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Variable declaration\n",
+ "a = int(raw_input('Enter first number : '))\n",
+ "original_a = a\n",
+ "\n",
+ "b = int(raw_input('Enter second number : '))\n",
+ "original_b = b\n",
+ "\n",
+ "# Calculation and result\n",
+ "c = 1\n",
+ "while (b) :\n",
+ "\tif (b & 1) :\n",
+ "\t\tc = c * a\n",
+ "\tb = b >> 1\n",
+ "\ta = a * a\n",
+ "\n",
+ "print ('%d raise to %d = %d ' % (original_a, original_b, c))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter first number : 7\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "Enter second number : 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "7 raise to 3 = 343 \n"
+ ]
+ }
+ ],
+ "prompt_number": 12
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file