First Summer 2006


Lecture Notes Eight: Catching up with ourselves.
Today in class we will discuss solutions for all the problems we have seen during the last few days.

We will try to post here any source code we develop in class.

The first program we wrote was for the "leap year" problem.

year = raw_input("Year: ")
year = int(year)

if year <= 1582:
    if year % 4 == 0:
        print "Leap year"
    else:
        print "Not a leap year"
else:
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                print "Leap year"
            else:
                print "Not a leap year"
        else:
            print "Leap year"
    else:
        print "Not a leap year"

The last one was about giving change:

due = raw_input("Due: ")
pay = raw_input("Pay: ")

change = int(100 * float(pay)) - int(100 * float(due))

print change / 25, " quarters"

change = change % 25

print change / 10, " dimes"

change = change % 10

print change / 5, " nickels"

change = change % 5

print change, " cents"

We also discussed this program:

sentence = raw_input("Enter sentence: ")

sentence = sentence.replace("e", "egge")
sentence = sentence.replace("a", "egga")
sentence = sentence.replace("i", "eggi")
sentence = sentence.replace("o", "eggo")
sentence = sentence.replace("u", "eggu")

print sentence

In particular we said it is fundamental what substitution we do first.

So the real solution, most general, is this:

sentence = raw_input("Enter sentence: ")

translation = ""
for e in sentence:
    if e in "aeiou":
        translation = translation + "aeiou" + e
    else:
        translation = translation + e

print translation 

This is just like in your Python textbook.


Last updated: May 17, 2006 by Adrian German for A201/A597