summaryrefslogtreecommitdiff
path: root/C_Programming:_A_Modern_Approach_by_K.N._King/Chapter13.ipynb
blob: 03c834504d399e84607007b5760977abb8ad8d38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
{
 "metadata": {
  "name": "",
  "signature": "sha256:00fb09fcd8819c39a88f9cac94eaa93d1e9e72c864c95cc17c2c19aa93c94143"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 13: Strings"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example remind.c, Page 294"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "MAX_REMIND=50\n",
      "MSG_LEN=60\n",
      "reminders = [['' for i in xrange(MSG_LEN+3)] for i in xrange(MAX_REMIND)]\n",
      "num_remind=0\n",
      "while(1):\n",
      "    if(num_remind==MAX_REMIND):\n",
      "        print \"--No space left --\"\n",
      "        break\n",
      "    li=raw_input( \"Enter day and reminder: \")\n",
      "    li1=li.split()\n",
      "    day_str=li1[0]\n",
      "    if(day_str=='0'):\n",
      "        break\n",
      "    msg_str=li1[1:]\n",
      "    i=0\n",
      "    for i in range(num_remind):\n",
      "        if(day_str<reminders[i]):\n",
      "            break\n",
      "    j=num_remind\n",
      "    while(j>i):\n",
      "        reminders[j]=reminders[j-1]\n",
      "        j=j-1\n",
      "    reminders[i]=day_str\n",
      "    reminders[i]=str(reminders[i])+str(msg_str)\n",
      "    \n",
      "    num_remind=num_remind+1\n",
      "print \"\"\n",
      "print \"Day Reminder\"\n",
      "for i in range(num_remind):\n",
      "    print \"%s\"%reminders[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example planet.c, Page 303"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "NUM_PLANETS=9\n",
      "li=raw_input()\n",
      "argv=li.split()\n",
      "planets=['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune','Pluto']\n",
      "argc=len(argv)\n",
      "for i in range(argc-1):\n",
      "    for j in range(NUM_PLANETS):\n",
      "        if( argv[i+1]==planets[j]):\n",
      "            print \"%s is a planet %d\"%(argv[i+1],(j+1))\n",
      "            break\n",
      "    if(argv[i] not in planets):\n",
      "        print \"%s is not a planet\"%argv[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "name": "stdout",
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "planet Jupiter Venus Earth fred\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Jupiter is a planet 5\n",
        "planet is not a planet\n",
        "Venus is a planet 2\n",
        "Earth is a planet 3\n"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}