Contents
- caesar_bycounter.py
- card2.py
- card.py
- counter2.py
- counter.py
- deck.py
caesar_bycounter.py 1/6
[top][prev][next]
# Name removed to protect the innocent
# Modified by CS111 to use Counter class
#
MIN_KEY=-25
MAX_KEY=25
A_VAL = ord('a')
Z_VAL = ord('z')
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, retaining spaces between words
def encoder(text, key):
message=""
for ch in text:
if ch == " ":
message+=" "
else:
message += translateLetter(ch,key)
return message
#takes as parameters a lowercase letter and integer key and returns the
#encoded letter
def translateLetter(letter, key):
# create a counter object with a range from the ASCII values of 'a' to 'z'
ascii_counter = Counter(ord('a'), ord('z'))
# set the counter to the letter to encode
ascii_counter.setValue(ord(letter))
# translate the ASCII value
if key > 0:
ascii_counter.increment(key)
else:
ascii_counter.decrement(key)
encoded_value = ascii_counter.getValue()
# translate from ASCII to the letter
transLetter = chr(encoded_value)
return transLetter
#call main function
main()
card2.py 2/6
[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):
"Returns the value of the card in Black Jack"
if self.rank == 14:
# alternatively, this could be 11
return 1
elif self.rank > 10:
return 10
else:
return self.rank
def rummyValue(self):
"Returns the value of the card in Rummy"
if self.rank <= 9:
return 5
elif self.rank < 14:
return 10
else:
return 15
def main():
c1 = Card(14, "spades")
print c1
print "Black Jack Value", c1.blackJackValue()
print "Rummy Value", c1.rummyValue()
c2 = Card(13, "hearts")
print c2
print "Black Jack Value", c2.blackJackValue()
print "Rummy Value", c2.rummyValue()
if __name__ == '__main__':
main()
card.py 3/6
[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 main():
c1 = Card(14, "spades")
print c1
c2 = Card(13, "hearts")
print c2
if __name__ == '__main__':
main()
counter2.py 4/6
[top][prev][next]
class Counter:
""" A class to represent a counter that wraps around from a high value back
to its low value."""
def __init__(self, low, high):
""" Creates a Counter object with the given low and high values """
self.low = low
self.high = high
self.value = low
def __str__(self):
"Returns a string representation of this Counter object."
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 by the given amount, wrapping around to low
again, if necessary. Returns number of times had to wrap around. """
range_of_nums = self.high-self.low+1
# the number of times this will wrap around
num_wraps = amount/range_of_nums
# the "real" amount to increment by
real_inc_amount = amount%range_of_nums
self.value += real_inc_amount
if self.value > self.high:
self.value = self.low + (self.value - self.high - 1)
return num_wraps
def decrement(self, amount=1):
""" Decrement the counter by the given amount (a positive number),
wrapping around to high again, if necessary. Returns number of times had
to wrap around. """
range_of_nums = self.high-self.low+1
num_wraps = amount/range_of_nums
real_dec_amount = amount % range_of_nums
self.value -= real_dec_amount
if self.value < self.low:
self.value = self.high - (self.low - self.value + 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
print "Increment by 20 --> wraps", c.increment(20), "times"
print c
c.decrement()
print c
print "Decrement by 20 --> wraps", c.decrement(20), "times"
print c
testCounter()
counter.py 5/6
[top][prev][next]
class Counter:
""" A class to represent a counter that wraps around from a high value back
to its low value."""
def __init__(self, low, high):
""" Creates a Counter object with the given low and high values """
self.low = low
self.high = high
self.value = low
def __str__(self):
"Returns a string representation of this Counter object."
rep = "Value: " + str(self.value)
rep += " Low: " + str(self.low)
rep += " High: " + str(self.high)
return rep
def increment(self, amount=1):
""" Increment the counter by the given amount, 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 by the given amount (a positive number),
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, -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
print "Increment by 20 --> wraps", c.increment(20), "times"
print c
c.decrement()
print c
print "Decrement by 20 --> wraps", c.decrement(20), "times"
print c
testCounter()
deck.py 6/6
[top][prev][next]
from card import *
from random import shuffle
class Deck:
""" A class to represent a deck of playing cards."""
def __init__(self):
"""Construct a new Deck object, containing the 52 standard playing
cards."""
self.cards = []
# executes 4 times (once for each suit)
for suit in ["clubs","hearts","diamonds","spades"]:
# executes 13 times (once for each rank)
for rank in range(2,15):
# the body of the loop gets executed a total of 52
# times
self.cards.append(Card(rank, suit))
def __str__(self):
"""Returns a string representation of the Deck."""
result = ""
for c in self.cards:
# Either of the following two lines do the same thing,
# have the same result:
#result += c.__str__() + "\n"
result += str(c) + "\n"
return result
def shuffle(self):
shuffle(self.cards)
def getNumCardsLeft(self):
"""Returns the number of cards left in the deck."""
return len(self.cards)
def drawCard(self):
"""Returns the top card on the deck. If there are no cards in
the deck, returns None"""
if self.getNumCardsLeft() == 0:
return None
return self.cards.pop(0)
def main():
d = Deck()
d.shuffle()
print d
main()
Generated by GNU enscript 1.6.4.