Contents

  1. card_byid.py
  2. card.py
  3. years_dictionary2.py

card_byid.py 1/3

[
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."
        # card ids go from 2 to 55
        self.cardid = rank
        if suit == "clubs":
            self.cardid += 13
        elif suit == "hearts":
            self.cardid += 26
        elif suit == "diamonds":
            self.cardid += 39

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

    def getRank(self):
        "Returns rank."
        return (self.cardid-2) % 13 + 2

    def getSuit(self):
        "Returns suit."
        suits = ["spades", "clubs", "hearts", "diamonds"]
        whichsuit = (self.cardid-2)//13
        return suits[whichsuit]

    def blackJackValue(self):
        "Returns the value of the card in Black Jack"
        if self.getRank() == 14:
            # alternatively, this could be 11
            return 1
        elif self.getRank() > 10:
            return 10
        else:
            return self.getRank()

    def rummyValue(self):
        "Returns the value of the card in Rummy"
        if self.getRank() <= 9:
            return 5
        elif self.getRank() < 14:
            return 10
        else:
            return 15

def main():
    c1 = Card(14, "spades")
    print( c1 )
    print( c1.getRank() )
    print( "Black Jack Value", c1.blackJackValue())
    print( "Rummy Value", c1.rummyValue())
    c2 = Card(13, "hearts")
    print(c2)
    print(c2.getRank())
    print("Black Jack Value", c2.blackJackValue())
    print("Rummy Value", c2.rummyValue())
    
if __name__ == '__main__':
    main()


card.py 2/3

[
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):
        """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 rank."	
        return self.rank

    def getSuit(self):
        "Returns suit."
        return self.suit
        
# Since I am probably going to import this script into another script,
# I only want to call main() when it's *not* imported
def main():
    c1 = Card(14, "spades")
    print(c1)
    c2 = Card(13, "hearts")
    print(c2)
    c3 = Card(2, "diamonds")
    print(c3)
    
    myString = c3.__str__()
    myString2 = str(c3)
    
    print(myString)
    print(myString2)
    
if __name__ == '__main__':
    main()

years_dictionary2.py 3/3

[
top][prev][next]
# Given a file of the form <lastname> <year>
# creates a mapping between the years and the number of students
# in that year.
# by CSCI 111

FILENAME="data/years.dat"

# open the data file for reading
yearsFile = open(FILENAME, "r")

# create an accumulator dictionary
yearToNumberOfStudents = {}

# iterate through the lines of the file
for line in yearsFile:
    # split the line
    # the list created has the last name as the first item
    # and the second item in the list is the year
    lineList = line.split()
    lastname = lineList[0]  # --> no longer need the last name
    year = int(lineList[1])
    
    # find out if there was a previous count
    #oldCount = yearToNumberOfStudents.get(year)
    
    # there was no mapping in the dictionary yet
    #if oldCount is None:
        # just found a student
    #    yearToNumberOfStudents[year] = 1
    #else:
    #    yearToNumberOfStudents[year] = oldCount + 1
        
    # Alternative solution:
    if year in yearToNumberOfStudents:
         yearToNumberOfStudents[year]+=1
    else:
         yearToNumberOfStudents[year] = 1
    
yearsFile.close()

print("The number of students enrolled in CSCI111 for each class year:")
print("%4s %5s" % ("Year", "Count"))
print("-"*4, "-"*5)
for year in sorted(yearToNumberOfStudents):
    count = yearToNumberOfStudents[year]
    print("%4d %5d" % (year, count))
    

Generated by GNU enscript 1.6.4.