summaryrefslogtreecommitdiff
path: root/The_Spirit_of_C/Chapter12.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'The_Spirit_of_C/Chapter12.ipynb')
-rwxr-xr-xThe_Spirit_of_C/Chapter12.ipynb455
1 files changed, 0 insertions, 455 deletions
diff --git a/The_Spirit_of_C/Chapter12.ipynb b/The_Spirit_of_C/Chapter12.ipynb
deleted file mode 100755
index 0195ed25..00000000
--- a/The_Spirit_of_C/Chapter12.ipynb
+++ /dev/null
@@ -1,455 +0,0 @@
-{
- "metadata": {
- "name": "",
- "signature": "sha256:bfcc6bf3e86f2ff3b103be284b1b027f6faa9020241e866b9b4398fd89f8923c"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "heading",
- "level": 1,
- "metadata": {},
- "source": [
- "Chapter 12, Strings and String Functions"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-1, Page Number: 331"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# The control string is just a string\n",
- "\n",
- "format = \"%s\"\n",
- "message = \"hello\"\n",
- "\n",
- "print format % message"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "hello\n"
- ]
- }
- ],
- "prompt_number": 1
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-2, Page Number: 334"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# The control string is just a string - part 2\n",
- "\n",
- "format = \"%.15s\"\n",
- "message = \"hellohellohello\"\n",
- "\n",
- "# Read in the number of characters to be displayed from \"message\"\n",
- "print \"How many characters do you want displayed?\",\n",
- "size = 5\n",
- "print size\n",
- "\n",
- "# Convert size to 2-digit number in format[2] and format[3]\n",
- "\n",
- "format = \"%.\" + str(size / 10) + str(size % 10) + \"s\"\n",
- "print \"The format is %s\"%format\n",
- "\n",
- "# Print the string\n",
- "print format % message"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "How many characters do you want displayed? 5\n",
- "The format is %.05s\n",
- "hello\n"
- ]
- }
- ],
- "prompt_number": 2
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-3, Page Number 336"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# sprintf function \n",
- "\n",
- "# There is no sprintf functionality in python\n",
- "# However an alternate way to solve the problem is...\n",
- "\n",
- "print \"Please enter a number:\",\n",
- "number = 17\n",
- "print number\n",
- "\n",
- "string = \"The number is %d\"%number\n",
- "\n",
- "print \"%s\"%string\n",
- "\n",
- "# OR \n",
- "\n",
- "print string"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "Please enter a number: 17\n",
- "The number is 17\n",
- "The number is 17\n"
- ]
- }
- ],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-4, Page Number 337"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Variable control string using the sprintf function\n",
- "\n",
- "message = \"hellohellohello\"\n",
- "\n",
- "# Read in the number of characters to be displayed from message \n",
- "print \"How many characters do you want displayed?\",\n",
- "size = 7\n",
- "print size\n",
- "\n",
- "# Create format in \"format\" using \"size\"\n",
- "format = \"%%.%ds\\n\"%size\n",
- "print \"The format is: %s\"%format\n",
- "\n",
- "# Print the string\n",
- "print format % message"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "How many characters do you want displayed? 7\n",
- "The format is: %.7s\n",
- "\n",
- "hellohe\n",
- "\n"
- ]
- }
- ],
- "prompt_number": 4
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-5, Page Number 338"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# A terse string copy function\n",
- "\n",
- "def strcpy(copy, original):\n",
- "\t\"\"\" copies one string into another \"\"\"\n",
- "\tcopy = original\n",
- "\treturn copy\n",
- "\n",
- "string1 = \"\"\n",
- "string1 = strcpy(string1, \"This is a message\")\n",
- "print \"String 1 = %s\"%string1\n",
- "\n",
- "string2 = \"\"\n",
- "string2 = strcpy(string2, string1)\n",
- "print \"String 2 = %s\"%string2"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "String 1 = This is a message\n",
- "String 2 = This is a message\n"
- ]
- }
- ],
- "prompt_number": 5
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-7, Page Number 344"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "string_point = \"hello\"\n",
- "\n",
- "string_var1 = string_point\n",
- "\n",
- "print \"%s\\n%s\"%(string_point,string_var1)\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "hello\n",
- "hello\n"
- ]
- }
- ],
- "prompt_number": 7
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-8, Page Number 346"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# strcat function in python\n",
- "# simple addition of strings results in concatenation in python\n",
- "\n",
- "def strcat(string1, string2) :\n",
- "\t\"\"\" concatenates the string and returns the concatenated string \"\"\"\n",
- "\tstring2 = string1 + string2\n",
- "\treturn string2\n",
- "\n",
- "string1 = \"hello\"\n",
- "string2 = strcat(string1,\" there\")\n",
- "print \"string1 = %s\\nstring2 = %s\"%(string1,string2)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string1 = hello\n",
- "string2 = hello there\n"
- ]
- }
- ],
- "prompt_number": 8
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-9, Page Number 348"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# strcat version 2\n",
- "\n",
- "def strcat(string1, string2) :\n",
- "\tstring1 = string1 + string2\n",
- "\tstring2 = string1\n",
- "\treturn string1,string2\n",
- "\n",
- "string1 = \"hello\"\n",
- "string1,string2 = strcat(string1, \" there\")\n",
- "\n",
- "print \"string1 = %s\\nstring2 = %s\"%(string1,string2)\n",
- "\n",
- "if string1 == string2 :\n",
- "\tprint \"string2 points to string1\"\n",
- "else :\n",
- "\tprint \"string2 does not point to string1\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "string1 = hello there\n",
- "string2 = hello there\n",
- "string2 points to string1\n"
- ]
- }
- ],
- "prompt_number": 9
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Program 12-10, Page Number 351"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Alphabetizing program using dynamically allocated arrays\n",
- "\n",
- "# Macro definition\n",
- "MAX_ARRAY_SIZE = 20\t\t# The array may have up to 20 elements\n",
- "MAX_STRING_SIZE = 10 \t# Each string may have up to 10 characters\n",
- "# Message flags for \"print_array\"\n",
- "ORIGINAL = 0\n",
- "SORTED = 1\n",
- "\n",
- "words = [ \"cat\", \"pig\", \"dog\", \"earlobe\", \"bingo\", \"elbow\", \"likely\", \"radar\" ]\n",
- "array_size = 0\n",
- "\n",
- "def get_size(size) :\n",
- "\t\"\"\" Read the size of the array into size (until a value is entered) \"\"\"\n",
- "\tprint \"How many words? \",\n",
- "\tsize = 8\n",
- "\tprint size\n",
- "\twhile size < 1 or size > MAX_ARRAY_SIZE :\n",
- "\t\tprint \"How many words? \",\n",
- "\t\tsize = 8\n",
- "\t\tprint size\n",
- "\tglobal array_size\n",
- "\tarray_size = size\n",
- "\n",
- "def get_array(array,size) :\n",
- "\t\"\"\" Read in the string array called 'array' of size 'size' \"\"\"\n",
- "\tprint \"Please enter the words:\"\n",
- "\tfor i in array :\n",
- "\t\tprint i\n",
- "\t\n",
- "def print_array(array,size,message) :\n",
- "\t\"\"\" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) \"\"\"\n",
- "\tprintout = [ \"\\nThe original array is:\", \"\\nThe sorted array is:\", \"\\n\" ]\n",
- "\t# Print the appropriate message\n",
- "\tprint \"%s\"%printout[message]\n",
- "\n",
- "\tfor i in array :\n",
- "\t\tprint i\n",
- "\n",
- "def pointer_sort(array,size) :\n",
- "\t\"\"\" sort string array \"\"\"\n",
- "\tfor i in range (size-1) :\n",
- "\t\tfor j in range (i + 1, size) :\n",
- "\t\t\tif array[i] > array[j] :\n",
- "\t\t\t\tswap(i,j)\n",
- "\n",
- "def swap(i, j) :\n",
- "\t\"\"\" Exchange strings \"\"\"\n",
- "\tglobal words\n",
- "\twords[i],words[j] = words[j],words[i]\n",
- "\n",
- "get_size(array_size)\t\t# Read in the array size\n",
- "get_array(words, array_size)\t# Read in the string array \"words\" of size \"array_size\"\n",
- "print_array(words, array_size, ORIGINAL)\t# Echo the original array\n",
- "pointer_sort(words, array_size)\t\n",
- "print_array(words, array_size, SORTED) # Print the sorted array"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [
- {
- "output_type": "stream",
- "stream": "stdout",
- "text": [
- "How many words? 8\n",
- "Please enter the words:\n",
- "cat\n",
- "pig\n",
- "dog\n",
- "earlobe\n",
- "bingo\n",
- "elbow\n",
- "likely\n",
- "radar\n",
- "\n",
- "The original array is:\n",
- "cat\n",
- "pig\n",
- "dog\n",
- "earlobe\n",
- "bingo\n",
- "elbow\n",
- "likely\n",
- "radar\n",
- "\n",
- "The sorted array is:\n",
- "bingo\n",
- "cat\n",
- "dog\n",
- "earlobe\n",
- "elbow\n",
- "likely\n",
- "pig\n",
- "radar\n"
- ]
- }
- ],
- "prompt_number": 10
- }
- ],
- "metadata": {}
- }
- ]
-}