{ "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_stri):\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": {} } ] }