Contents

  1. caesar2.py
  2. caesar.py
  3. card3.py
  4. clock.py
  5. counter.py

caesar2.py 1/5

[
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/5

[
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()

card3.py 3/5

[
top][prev][next]
class Card:
    """
    A class to represent a standard playing card. The ranks are ints:
    2-10 for numbered cards, 11=Jack, 12=Queen, 13=King, 14=Ace.
    The suits are strings: 'clubs', 'spades', 'hearts', 'diamonds'.
    """
    
    def __init__(self, rank, suit):
        "Constructor for class Card takes int rank and string suit."
        self.rank = rank
        self.suit = suit

    def __str__(self):
        "Returns a string describing the card as 'rank of suit'."
        result = ""
        if self.rank == 11:
            result += "Jack"
        elif self.rank == 12:
            result += "Queen"
        elif self.rank == 13:
            result += "King"
        elif self.rank == 14:
            result += "Ace"
        else:
            result += str(self.rank)
        result += " of " + self.suit
        return result

    def getRank(self):
	"Returns rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit

    def blackJackValue(self):
        if self.rank == 14:
            return 1
        elif self.rank > 10:
            return 10
        else:
            return self.rank

    def rummyValue(self):
        if self.rank == 14:
            return 15
        elif self.rank > 10:
            return 10
        else:
            return 5
    
    def __cmp__(self, other):
        """ Compares Card objects by their ranks """
        # Could compare by black jack value or rummy value
        #print self, other
        if self.rank < other.getRank():
         #   print "less"
            return -1
        elif self.rank > other.getRank():
          #  print "greater"
            return 1
        else:
            return 0
        

def main():
    c1 = Card(14, "spades")
    print c1
    print "Black Jack Value", c1.blackJackValue()
    c2 = Card(13, "hearts")
    print c2
    print "Black Jack Value", c2.blackJackValue()
    c3 = Card(3, "diamonds")
    print c3
    print "Rummy value", c3.rummyValue()
    
    print c1.__cmp__(c3)
    print cmp(c1,c3)
    print c1,"<",c3,"?", c1 < c3
    print c1,">",c3,"?", c1 > c3
    
if __name__ == '__main__':
    main()

clock.py 4/5

[
top][prev][next]
# Class representing a Clock
# By CS111 class
# 11.02.07

from counter import *

class Clock:
    """ A class representing a clock """
    
    def __init__(self, h=12, m=0, s=0):
        """ Create an instance of the Clock class.  Can either call with
        or without the integer parameters h (for hours), m (for minutes), or 
        s (for seconds).  If no parameters are defined, clock defaults to 12:00:00.
        Pre: 1 <= h <= 12, 0 <= m <= 59, 0 <= s <= 59
        """
        self.hours = Counter(1,12)
        self.hours.setValue(h)
        self.minutes = Counter(0,59)
        self.minutes.setValue(m)
        self.seconds = Counter(0,59)
        self.seconds.setValue(s)
    
    def __str__(self):
        "Returns a string representing the class as hh:mm:ss"
        h =self.hours.getValue()
        m = self.minutes.getValue()
        s = self.seconds.getValue()
        result = "%d:%02d:%02d" % (h,m,s)
        return result
        
    def tick(self):
        "Increase the time by one second"
        self.seconds.increment()
        if self.seconds.getValue() == self.seconds.getLow():
            self.minutes.increment()
            if self.minutes.getValue() == self.minutes.getLow():
                self.hours.increment()
    
    def setTime(self, hours, minutes, seconds):
        """Sets the current time with integer parameters hours, minutes, and seconds.
        Pre: 1 <= h <= 12, 0 <= m <= 59, 0 <= s <= 59
        """
         self.hours.setValue(hours)
         self.minutes.setValue(minutes)
         self.seconds.setValue(seconds)
    
# function to test the clock class
def testClock():
    clock = Clock()
    print clock
    clock.tick()
    print clock
    for x in xrange(100):
        clock.tick()
    print clock
    clock.setTime(12,59,59)
    print clock
    clock.tick()
    print clock
    # check if bad values to set clock to
    clock.setTime(-1,60,0)



testClock()

counter.py 5/5

[
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()

Generated by GNU enscript 1.6.4.