Contents

  1. caesar2.py
  2. caesar.py
  3. counter.py
  4. groupShift.py

caesar2.py 1/4

[
top][prev][next]
# Name removed to protect the innocent
# Modified by CS111 to use Counter class
#

from counter import *

MIN_KEY=-25
MAX_KEY=25

def main():
    text=raw_input("Enter some text: ")
    key=input("Enter an integer key (between "+ str(MIN_KEY) +" and " + str(MAX_KEY) +"): ")
    if key < MIN_KEY or key > MAX_KEY:
        print "Key must be within range", MIN_KEY,"and", MAX_KEY
    else: 
        text = text.lower()
        message= encoder(text,key)
        print message

#encoder takes in some lowercase text and integer key and returns encoded
#message, ignoring spaces in message
def encoder(text,key):
    message=""
    for ch in text:
        if ch == " ":
            message+=" "
        else:
            message += translateLetter(ch,key)
    return message

#translateletter takes in a lowercase letter and integer key and returns the
#encoded letter
def translateLetter(letter,key):
    # create a counter object with a range from 'a' to 'z'
    cipher = Counter(ord('a'), ord('z'))
    cipher.setValue(ord(letter))
    if key > 0:
        cipher.increment(key)
    else:
        cipher.decrement(key)
    encode=chr(cipher.getValue())
    return encode
            
#call main function
main()

caesar.py 2/4

[
top][prev][next]
# Name removed to protect the innocent
# Modified by CS111 to use Counter class
#

MIN_KEY=-25
MAX_KEY=25

def main():
    text=raw_input("Enter some text: ")
    key=input("Enter an integer key (between "+ str(MIN_KEY) +" and " + str(MAX_KEY) +"): ")
    if key < MIN_KEY or key > MAX_KEY:
        print "Key must be within range", MIN_KEY,"and", MAX_KEY
    else: 
        text = text.lower()
        message= encoder(text,key)
        print message

#encoder takes in some lowercase text and integer key and returns encoded
#message, ignoring spaces
def encoder(text,key):
    message=""
    for ch in text:
        if ch == " ":
            message+=" "
        else:
            message += translateLetter(ch,key)
    return message

#translateletter takes in a lowercase letter and integer key and returns the
#encoded letter
def translateLetter(letter,key):
    codedletter=ord(letter)+key
    A_VAL = ord('a')
    Z_VAL = ord('z')
    if codedletter > Z_VAL:
        encode = chr((codedletter-Z_VAL)+A_VAL-1)
    elif codedletter < A_VAL:
        encode = chr(Z_VAL-(A_VAL-codedletter)+1)
    else:
        encode=chr(codedletter)
    return encode
            
#call main function
main()

counter.py 3/4

[
top][prev][next]
class Counter:
    def __init__(self, low, high):
        self.low = low
        self.high = high
        self.value = low

    def __str__(self):
        result = "Value: " + str(self.value)
        result += " Low: " + str(self.low)
        result += " High: " + str(self.high)
        return result

    def increment(self, amount=1):
        """ increment the counter, wrapping around to low again, if necessary.
        Returns number of times had to wrap around. """
        num_wraps = 0
        for x in xrange(amount):
            if self.value < self.high:
                self.value += 1
            else:
                self.value = self.low
                num_wraps += 1
        return num_wraps
        
    def decrement(self, amount=-1):
        """ decrement the counter, wrapping around to high again, if necessary.
        Returns number of times had to wrap around. """
        num_wraps = 0
        for x in xrange(0, amount, -1):
            if self.value > self.low:
                self.value -= 1
            else:
                self.value = self.high
                num_wraps -= 1
        return num_wraps

    def setValue(self, value):
        """ Sets the counter's value, only if low <= value <= high.  Otherwise,
        prints an error message."""
        if value >= self.low and value <= self.high:
            self.value = value
        else:
            print "ERROR: value must be within range", self.low, "-", \
                  self.high

    def getValue(self):
        return self.value

    def getLow(self):
        return self.low

    def getHigh(self):
        return self.high

def testCounter():
    c = Counter(5,10)
    print c
    c.increment()
    print c
    c.increment(3)
    print "Current value:", c.getValue()
    c.increment(4)
    print c


#testCounter()

groupShift.py 4/4

[
top][prev][next]
# This program draws an initial circle in the graphics window.
# Each time the user clicks on a new spot, the circle is
# shifted to that location.  This happens four times. On the fifth
# mouse click the program ends.

from graphics import *

class ShapesGroup:
    def __init__(self, list):
        self.listOfShapes = list
        
    def move(self, dx, dy):
        for x in self.listOfShapes:
            x.move(dx,dy)

def main():
    win = GraphWin("Moving A Group of Shapes")
    circ = Circle(Point(50,50), 20)
    circ.setOutline("black")
    circ.setFill("magenta")
    circ.draw(win)
    
    circ2 = circ.clone()
    circ2.move(10,10)
    circ2.draw(win)
    
    group = ShapesGroup([circ,circ2])

    label = Text(Point(100,180), "Click where the circle goes")
    label.draw(win)
    
    for i in range(5):
        moveTo = win.getMouse()
        current = circ.getCenter()
        dx = moveTo.getX() - current.getX()
        dy = moveTo.getY() - current.getY()
        group.move(dx,dy)
        label.setText( str(5-i-1) + " clicks left")


main()
                   

Generated by GNU enscript 1.6.4.