Contents
- ./card2.py
- ./card.py
- ./grad_years.py
./card2.py 1/3
[top][prev][next]
# Card class and demonstration of use.
# Difference from card.py: additional methods implemented and tested
# by CSCI111
import test
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):
"Constructs a new Card object with the given rank (an int) and suit (a string)."
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 this card's rank."
return self._rank
def getSuit(self):
"Returns this card's suit."
return self._suit
def getCardColor(self):
"""
Returns the color of the card's suit.
This will be "red" for hearts and diamonds and
"black" for spades and clubs
"""
if self._suit == "diamonds":
result = "red"
#return "red"
elif self._suit == "hearts":
result = "red"
else:
result = "black"
return result
def getRummyValue(self):
"""
Returns the value of the card in a game
of Rummy. (Apparently, Sprenkle modified rules)
"""
if self._rank == 14:
return 15
elif self._rank < 11:
return 5
else:
return 10
def testCardClass():
"""
Exercises many methods of the Card class.
Note that this test function was built up over time, testing one method
at a time and then adding tests for the next method, repeat.
"""
c1 = Card(14, "spades")
c2 = Card(13, "hearts")
c3 = Card(2, "diamonds")
# test the getSuit() method and constructor
test.testEqual(c1.getSuit(), "spades")
test.testEqual(c2.getSuit(), "hearts")
test.testEqual(c3.getSuit(), "diamonds")
# test the getRank() method and constructor
test.testEqual(c1.getRank(), 14)
test.testEqual(c2.getRank(), 13)
test.testEqual(c3.getRank(), 2)
# test the __str__ method
test.testEqual( str(c1), "Ace of spades")
test.testEqual( str(c2), "King of hearts")
test.testEqual( str(c3), "2 of diamonds")
# test the getCardColor method
test.testEqual( c1.getCardColor(), "black")
test.testEqual( c2.getCardColor(), "red")
# We can test a method with a bunch of objects all at once.
# You set up your objects and expected results in a list.
# It's easy to add new test cases to the list.
myCards = [c1, c2, c3]
expectedSuitResults = ["spades", "hearts", "diamonds"]
for index in range(len(myCards)):
test.testEqual( myCards[index].getSuit(), expectedSuitResults[index])
expectedRankResults = [14, 13, 2]
expectedStrResults = ["Ace of spades", "King of hearts", "2 of diamonds"]
expectedColorResults = ["black", "red", "red"]
# should add a test for clubs
# tests a bunch of methods at once
for index in range(len(myCards)):
test.testEqual( myCards[index].getSuit(), expectedSuitResults[index])
test.testEqual( myCards[index].getRank(), expectedRankResults[index])
test.testEqual( str(myCards[index]), expectedStrResults[index])
test.testEqual( myCards[index].getCardColor(), expectedColorResults[index])
def main():
# exercise the class
c1 = Card(14, "spades")
# display the card to see how the __str__ method output looks
print("Card 1:", c1)
c2 = Card(13, "hearts")
print("Card 2:", c2)
c3 = Card(2, "diamonds")
print("Card 3:", c3)
print("Card 3's suit:", c3.getSuit())
print("Card 3's rank:", c3.getRank())
# showing other ways to call the str method
myString = c3.__str__()
myString2 = str(c3)
print(myString)
print(myString2)
# exercise the getCardColor method
cardColor = c1.getCardColor()
print("Card 1's card color:", cardColor)
myHand = [ c1, c2, c3 ]
print()
print("My Hand:")
# Problem: determine the rummy value of this hand
rummyValue = 0
for card in myHand:
print(card)
rummyValue += card.getRummyValue()
print("My hand's value in Rummy is", rummyValue)
# Since I am probably going to import this class into another script,
# I only want to call main() when it's *not* imported
if __name__ == '__main__':
main()
testCardClass()
./card.py 2/3
[top][prev][next]
# Card class and demonstration of use
# by CSCI111
import test
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):
"Constructs a new Card object with the given rank (an int) and suit (a string)."
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 this card's rank."
return self._rank
def getSuit(self):
"Returns this card's suit."
return self._suit
def testCardClass():
"""
Exercises many methods of the Card class.
Note that this test function was built up over time, testing one method
at a time and then adding tests for the next method, repeat.
"""
c1 = Card(14, "spades")
c2 = Card(13, "hearts")
c3 = Card(2, "diamonds")
# test the getSuit() method and constructor
test.testEqual(c1.getSuit(), "spades")
test.testEqual(c2.getSuit(), "hearts")
test.testEqual(c3.getSuit(), "diamonds")
# test the getRank() method and constructor
test.testEqual(c1.getRank(), 14)
test.testEqual(c2.getRank(), 13)
test.testEqual(c3.getRank(), 2)
# test the __str__ method
test.testEqual( str(c1), "Ace of spades")
test.testEqual( str(c2), "King of hearts")
test.testEqual( str(c3), "2 of diamonds")
# We can test a method with a bunch of objects all at once.
# You set up your objects and expected results in a list.
# It's easy to add new test cases to the list.
myCards = [c1, c2, c3]
expectedSuitResults = ["spades", "hearts", "diamonds"]
for index in range(len(myCards)):
test.testEqual( myCards[index].getSuit(), expectedSuitResults[index])
expectedRankResults = [14, 13, 2]
expectedStrResults = ["Ace of spades", "King of hearts", "2 of diamonds"]
# tests a bunch of methods at once
for index in range(len(myCards)):
test.testEqual( myCards[index].getSuit(), expectedSuitResults[index])
test.testEqual( myCards[index].getRank(), expectedRankResults[index])
test.testEqual( str(myCards[index]), expectedStrResults[index])
def main():
# exercise the class
c1 = Card(14, "spades")
# display the card to see how the __str__ method output looks
print("Card 1:", c1)
c2 = Card(13, "hearts")
print("Card 2:", c2)
c3 = Card(2, "diamonds")
print("Card 3:", c3)
print("Card 3's suit:", c3.getSuit())
print("Card 3's rank:", c3.getRank())
print(c3)
# showing other ways to call the str method
myString = c3.__str__()
myString2 = str(c3)
print(myString)
print(myString2)
# Since I am probably going to import this class into another script,
# I only want to call main() when it's *not* imported
if __name__ == '__main__':
main()
testCardClass() # once testing is complete, we can remove this call
./grad_years.py 3/3
[top][prev][next]
# Given a file of the form <firstname> <classyear>,
# report how many students are in each class year
# by CSCI 111
FILENAME="data/roster.dat"
def main():
grad_year_to_count = count_students_in_each_graduation_year(FILENAME)
# Quick check of dictionary's contents
print(grad_year_to_count)
for gradYear in sorted(grad_year_to_count):
print(gradYear, grad_year_to_count[gradYear])
def count_students_in_each_graduation_year(filename):
"""
Parses the filename, which is in the form
name gradyear
name gradyear
...
Returns a dictionary that maps the graduation year to the number of
students in that graduation year
"""
# initialize our dictionary of accumulators
grad_year_to_num_students = {}
# open file
year_file = open(filename, "r")
# repeat reading a line from the file
for line in year_file:
# split the line into a list -- default delimiter is a space
contents = line.split()
# the first element is the name, the second is the grad year
grad_year = contents[1]
# is the grad year already in my dictionary?
if grad_year in grad_year_to_num_students:
grad_year_to_num_students[grad_year] += 1
# update (add one) to the value for that key (grad year)
# if not
else:
# add a mapping of grad year to the value of 1
grad_year_to_num_students[grad_year] = 1
print(grad_year_to_num_students)
# close file
year_file.close()
return grad_year_to_num_students
main()
Generated by GNU Enscript 1.6.5.90.