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>
# Keep track of the number of students in each year.
# by CSCI 111, 03.16.2012

FILENAME="data/years.dat"

lastNameToClassYear = {}

namesFile = open(FILENAME, "r")
classYearToCount = {}

for line in namesFile:
    # add each line (as a mapping) to the dictionary
    dataList = line.split()
    year = dataList[1]
    print(year)
    if year not in classYearToCount:
        classYearToCount[year] = 1
    else:
        classYearToCount[year] += 1
    print(classYearToCount)
    
namesFile.close()

# display the mappings, in order by class year (alphabetical order)
sortedClassYears = list(classYearToCount.keys())
sortedClassYears.sort()

for classYear in sortedClassYears:
    print("%18s %3s" % (classYear, classYearToCount.get(classYear)))
    

Generated by GNU enscript 1.6.4.