Chapter 13: Strings

Example remind.c, Page 294

In [2]:
MAX_REMIND=50
MSG_LEN=60
reminders = [['' for i in xrange(MSG_LEN+3)] for i in xrange(MAX_REMIND)]
num_remind=0
while(1):
    if(num_remind==MAX_REMIND):
        print "--No space left --"
        break
    li=raw_input( "Enter day and reminder: ")
    li1=li.split()
    day_str=li1[0]
    if(day_str=='0'):
        break
    msg_str=li1[1:]
    i=0
    for i in range(num_remind):
        if(day_str<reminders[i]):
            break
    j=num_remind
    while(j>i):
        reminders[j]=reminders[j-1]
        j=j-1
    reminders[i]=day_str
    reminders[i]=str(reminders[i])+str(msg_str)
    
    num_remind=num_remind+1
print ""
print "Day Reminder"
for i in range(0,num_remind):
    print "%s"%reminders[i]
Enter day and reminder: 24 Susan's Birthday
Enter day and reminder: 5 6:00 - Dinner with Marge and Russ
Enter day and reminder: 26 Movie - "Chinatown"
Enter day and reminder: 7 10:30 - Dental appointment
Enter day and reminder: 12 Movie - "Dazed and Confused"
Enter day and reminder: 5 Saturday class
Enter day and reminder: 12 Saturday class
Enter day and reminder: 0

Day Reminder
12['Saturday', 'class']
12['Movie', '-', '"Dazed', 'and', 'Confused"']
26['Movie', '-', '"Chinatown"']
5['Saturday', 'class']
5['6:00', '-', 'Dinner', 'with', 'Marge', 'and', 'Russ']
7['10:30', '-', 'Dental', 'appointment']
24["Susan's", 'Birthday']

Example planet.c, Page 303

In [5]:
NUM_PLANETS=9
li=raw_input()
argv=li.split()
planets=['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune','Pluto']
argc=len(argv)
for i in range(argc-1):
    for j in range(NUM_PLANETS):
        if( argv[i+1]==planets[j]):
            print "%s is a planet %d"%(argv[i+1],(j+1))
            break
    if(argv[i] not in planets):
        print "%s is not a planet"%argv[i]
planet Jupiter Venus Earth fred
Jupiter is a planet 5
planet is not a planet
Venus is a planet 2
Earth is a planet 3
In [ ]: