First Summer 2006


Lecture Notes Twenty-Three: Review of Chapter Eight
Here are the solutions for the problems in Homework Eight:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def distanceTo(self, other):
        dx = self.x - other.x
        dy = self.y - other.y
        return (dx**2+dy**2)**0.5

p = Point(2, 1)
q = Point(1, 2)
print p.distanceTo(q)
print p.distanceTo(Point(0, 0))

With Point behind us let's implement Line:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def distanceTo(self, other):
        dx = self.x - other.x
        dy = self.y - other.y
        return (dx**2+dy**2)**0.5

class Line(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def length(self):
        return self.a.distanceTo(self.b)

p = Point(2, 1)
q = Point(1, 2)
print p.distanceTo(q)
print p.distanceTo(Point(0, 0))
m = Line(p, q)
print m.length()

We will continue to carry old code along when producing new definitions:

class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def distanceTo(self, other):
        dx = self.x - other.x
        dy = self.y - other.y
        return (dx**2+dy**2)**0.5

class Line(object):
    def __init__(self, a, b):
        self.a = a
        self.b = b
    def length(self):
        return self.a.distanceTo(self.b)

class Triangle(object):
    def __init__(self, a, b, c):
        self.s1 = Line(a, b)
        self.s2 = Line(b, c)
        self.s3 = Line(c, a)
    def area(self):
        s = (self.s1.length() + self.s2.length() + self.s3.length())/2
        a = self.s1.length()
        b = self.s2.length()
        c = self.s3.length()
        return (s * (s - a) * (s - b) * (s - c)) ** 0.5

p = Point(2, 1)
q = Point(1, 2)
print p.distanceTo(q)
print p.distanceTo(Point(0, 0))
m = Line(p, q)
print m.length()
u = Triangle(Point(2, 2), Point(2, 5), Point(6, 2))
print u.area()

This concludes the series of first three problems (which builds upon each other).

Now the next problem was submitted by everybody so you can be sure it will be on the exam.

class Clock(object):
    def __init__(self, time):
        self.hour = int(time[0:2])
        self.minute = int(time[3:])
    def tick(self):
        self.minute = (self.minute + 1) % 60
        if self.minute == 0:
            self.hour = (self.hour + 1) % 24
        self.report()
    def report(self):
        print ("00" + str(self.hour))[-2:] + ":" + ("00" + str(self.minute))[-2:]

clock = Clock("23:58")
clock.tick()
clock.tick()
clock.tick()
clock.tick()
clock.tick()

The problem above was the one problem everybody submitted.

Nobody even acknowledged Lab Notes Eight. No, you just solved it and posted it.

Well done! You have one exam problem in your pocket.

import random

class Player(object):
    def __init__(self, name):
        self.guess = ""
        self.name = name
    def makeGuess(self):
        self.guess = random.choice(("paper", "rock", "scissors"))
    def report(self):
        return "Player " + self.name + " has chosen " + self.guess
    def strongerThan(self, other):
        if other.guess == "paper" and self.guess == "rock" or \
           other.guess == "rock" and self.guess == "scissors" or \
           other.guess == "scissors" and self.guess == "paper":
            return True
        else:
            return False

a = Player("Jordan")
b = Player("Bird")
for i in range(4): 
    a.makeGuess()
    print a.report()
    b.makeGuess()
    print b.report()
    print "Player " + a.name + " stronger than " + b.name + "? Answer: " + str(a.strongerThan(b))
    print "Player " + b.name + " stronger than " + a.name + "? Answer: " + str(b.strongerThan(a))
    print "------------------------------"

Here's a typical output for this:

>>> 
Player Jordan has chosen rock
Player Bird has chosen paper
Player Jordan stronger than Bird? Answer: 1
Player Bird stronger than Jordan? Answer: 0
------------------------------
Player Jordan has chosen paper
Player Bird has chosen scissors
Player Jordan stronger than Bird? Answer: 1
Player Bird stronger than Jordan? Answer: 0
------------------------------
Player Jordan has chosen scissors
Player Bird has chosen scissors
Player Jordan stronger than Bird? Answer: 0
Player Bird stronger than Jordan? Answer: 0
------------------------------
Player Jordan has chosen rock
Player Bird has chosen scissors
Player Jordan stronger than Bird? Answer: 0
Player Bird stronger than Jordan? Answer: 1
------------------------------

So the score should be now: Bird one, Jordan two, one tie.

The next one is the elevator.

class Elevator(object):
    def __init__(self, floor):
        self.floor = floor
    def up(self, to):
        if to > self.floor:
            while to > self.floor:
                self.floor += 1
                self.report()
        else:
            print "Sorry from floor " + str(self.floor) + " you can't go up to " + str(to)
    def down(self, to):
        if to < self.floor:
            while to < self.floor:
                self.floor -= 1
                self.report()
        else:
            print "Sorry from floor " + str(self.floor) + " you can't go down to " + str(to)
    def currentFloor(self):
        return self.floor
    def report(self):
        print "The elevator is currently on floor " + str(self.floor)

e = Elevator(20); 
e.up(26); 
e.down(14); 
e.up(10); 
e.down(30); 
e.up(e.currentFloor() + 3); 

This produces the following output:

The elevator is currently on floor 21
The elevator is currently on floor 22
The elevator is currently on floor 23
The elevator is currently on floor 24
The elevator is currently on floor 25
The elevator is currently on floor 26
The elevator is currently on floor 25
The elevator is currently on floor 24
The elevator is currently on floor 23
The elevator is currently on floor 22
The elevator is currently on floor 21
The elevator is currently on floor 20
The elevator is currently on floor 19
The elevator is currently on floor 18
The elevator is currently on floor 17
The elevator is currently on floor 16
The elevator is currently on floor 15
The elevator is currently on floor 14
Sorry from floor 14 you can't go up to 10
Sorry from floor 14 you can't go down to 30
The elevator is currently on floor 15
The elevator is currently on floor 16
The elevator is currently on floor 17

Which you can improve on, if you so desire.

The Robot exercise is next:

class Robot(object):
    def __init__(self, name, x, y, direction):
        self.name = name
        self.x = x
        self.y = y
        self.direction = direction
    def moveForward(self):
        print "Robot",self.name,"now moves forward."
        if self.direction == "north":
            self.y -= 1
        elif self.direction == "east":
            self.x += 1
        elif self.direction == "south":
            self.y += 1
        elif self.direction == "west":
            self.x -= 1
        else:
            print "I bet you don't expect to see this"
    def turnLeft(self):
        print "Robot",self.name,"now turns left."
        if self.direction == "north":
            self.direction = "west"
        elif self.direction == "east":
            self.direction = "north"
        elif self.direction == "south":
            self.direction = "east"
        elif self.direction == "west":
            self.direction = "south"
        else:
            print "I bet you don't expect to see this"
    def report(self):
        print "Robot",self.name,"located at (" + self.getX() + ", " + self.getY() +") facing", self.direction
    def getX(self):
        return str(self.x)
    def getY(self):
        return str(self.y)


a = Robot("Alice", 2, 3, "north")
a.report()

q = Robot("Queen", -4, -1, "west")
q.report()

a.turnLeft()
a.report()

a.moveForward()
a.report()

a.turnLeft()
a.report()

a.moveForward()
a.report()

a.moveForward()
a.report()

a.moveForward()
a.report()

q.moveForward()
q.report()

q.turnLeft()
q.report()

The code above produces:

Robot Alice located at (2, 3) facing north
Robot Queen located at (-4, -1) facing west
Robot Alice now turns left.
Robot Alice located at (2, 3) facing west
Robot Alice now moves forward.
Robot Alice located at (1, 3) facing west
Robot Alice now turns left.
Robot Alice located at (1, 3) facing south
Robot Alice now moves forward.
Robot Alice located at (1, 4) facing south
Robot Alice now moves forward.
Robot Alice located at (1, 5) facing south
Robot Alice now moves forward.
Robot Alice located at (1, 6) facing south
Robot Queen now moves forward.
Robot Queen located at (-5, -1) facing west
Robot Queen now turns left.
Robot Queen located at (-5, -1) facing south

And finally, the Tigger class:

import random

class Tigger(object):
    def __init__(self):
        self.x = str(random.randrange(1000))
        self.y = str(random.randrange(1000))
        self.report() 
    def bounce(self):
        x = 0
        for c in self.x:
            x += int(c)**2
        self.x = str(x)
        y = 0
        for c in self.y:
            y += int(c)**2
        self.y = str(y)
            
    def report(self):
        print "Tigger is now at (" + self.x + ", " + self.y + ")"

t = Tigger()
for i in range(10):
    t.bounce()
    t.report()

The code above produces:

Tigger is now at (168, 425)
Tigger is now at (101, 45)
Tigger is now at (2, 41)
Tigger is now at (4, 17)
Tigger is now at (16, 50)
Tigger is now at (37, 25)
Tigger is now at (58, 29)
Tigger is now at (89, 85)
Tigger is now at (145, 89)
Tigger is now at (42, 145)
Tigger is now at (20, 42)


Last updated: June 10, 2006 by Adrian German for A201/A597