Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is in Python! 8.32 Implement class Hand that represents a hand of playing c

ID: 667208 • Letter: T

Question

This is in Python!

8.32 Implement class Hand that represents a hand of playing cards. The class should have a
constructor that takes as input the player ID (a string). It should support method addCard()
that takes a card as input and adds it to the hand and method showHand() that displays the
player’s hand in the format shown.
>>> hand = Hand('House')
>>> deck = Deck()
>>> deck.shuffle()
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.addCard(deck.dealCard())
>>> hand.showHand()
House: 10 8 2

8.34 Implement class Date that support methods:
• __init__(): Constructor that takes no input and initializes the Date object to the
current date
• display(): Takes a format argument and displays the date in the requested format
Use function localtime() from the Standard Library module time to obtain the current
date. The format argument is a string
• ’MDY’ : MM/DD/YY (e.g., 02/18/09)
• ’MDYY’ : MM/DD/YYYY (e.g., 02/18/2009)
• ’DMY’ : DD/MM/YY (e.g., 18/02/09)
• ’DMYY’ : DD/MM/YYYY (e.g., 18/02/2009)
• ’MODY’ : Mon DD, YYYY (e.g., Feb 18, 2009)
You should use methods localtime() and strftime() from Standard Library module
time.
>>> x = Date()
>>> x.display('MDY')
'02/18/09'
>>> x.display('MODY')
'Feb 18, 2009'

8.35 Develop a class Craps that allows you to play craps on your computer. (The craps
rules are described in Problem 6.31.) Your class will support methods:
• __init__(): Starts by rolling a pair of dice. If the value of the roll (i.e., the sum of
the two dice) is 7 or 11, then a winning message is printed. If the value of the roll
is 2, 3, or 12, then a losing message is printed. For all other roll values, a message
telling the user to throw for point is printed.
• forPoint(): Generates a roll of a pair of dice and, depending on the value of the
roll, prints one of three messages as appropriate (and as shown):
>>> c = Craps()
Throw total: 11. You won!
>>> c = Craps()
Throw total: 2. You lost!

8.39 Write a container class called PriorityQueue. The class should supports methods:
• insert(): Takes a number as input and adds it to the container
• min(): Returns the smallest number in the container
• removeMin(): Removes the smallest number in the container
• isEmpty(): Returns True if container is empty, False otherwise
The overloaded operator len() should also be supported.
>>> pq = PriorityQueue()
>>> pq.insert(3)
>>> pq.insert(1)
>>> pq.insert(5)
>>> pq.insert(2)
>>> pq.min()
1
>>> pq.removeMin()
>>> pq.min()
2
>>> pq.size()
3
>>> pq.isEmpty()
False

Explanation / Answer

8.39


class PriorityQueue:
    """
    """

    def __init__(self, pqueue=list()):
        """
        """
        self.pq = pqueue

    def insert(self, other):
        """ Takes a number as input and adds it to the container
        """
        self.pq.append(other)

    def min(self):
        """ Returns the smallest number in the container
        """
        return min(self.pq)

    def removemin(self):
        """ Removes the smallest number in the container
        """
        self.pq.remove(min(self.pq))

    def isempty(self):
        """ Returns True if container is empty
        """
        return self.pq == [] or self.pq is None

    def __len__(self):
        """ Returns number of objects in pq
        """
        return len(self.pq)

pq = PriorityQueue()
pq.insert(3)
pq.insert(1)
pq.insert(5)
pq.insert(2)

print(pq.min())
pq.removemin()
print(pq.min())
print(len(pq))
print(pq.isempty())

8.35

import random


class Craps:
    """
    """

    def __init__(self):
        """
        """

        # first roll
        self.rounds = 1
        d1 = random.randrange(1, 7)
        d2 = random.randrange(1, 7)
        self.first_result = d1 + d2

        print('Round {} Throw total: {}'.format(self.rounds, self.first_result))

        if self.first_result in {7, 11}:
            print('You won!')
        elif self.first_result in {2, 3, 12}:
            print('You lost!')
        else:
            print('Throw for Point.')
            self.for_point()

    def for_point(self):
        """
        """
        # consecutive rolls
        d1 = random.randrange(1, 7)
        d2 = random.randrange(1, 7)
        next_result = d1 + d2
        self.rounds += 1

        print('Round {} Throw total: {}'.format(self.rounds, next_result))

        if next_result == 7:
            print('You lost')
        elif next_result == self.first_result:
            print('You won')
        else:
            print('Throw for Point.')
            self.for_point()

crap = Craps()

8.34


class Date:
    """
    """
    def __init__(self):
        """ Constructor: initializes date
        """
        self.date = time.localtime()

    def display(self, form):
        """ Takes form and returns sting based on form
        """
        intmonth = time.strftime('%m', self.date)
        abrevmonth = time.strftime('%b', self.date)
        intday = time.strftime('%d', self.date)
        intyear = time.strftime('%y', self.date)
        fullyear = time.strftime('%Y', self.date)
        formats = {'MDY': '{}/{}/{}'.format(intmonth, intday, intyear),
                   'MDYY': '{}/{}/{}'.format(intmonth, intday, fullyear),
                   'DMY': '{}/{}/{}'.format(intday, intmonth, intyear),
                   'DMYY': '{}/{}/{}'.format(intday, intmonth, fullyear),
                   'MODY': '{} {}, {}'.format(abrevmonth, intday, fullyear)}

        return formats[form]

x = Date()
print(x.display('MDY'))
print(x.display('MODY'))
8.32


class Hand:
    """ Represents a hand of playing cards
    """
    def __init__(self, pid=''):
        """ Constructor: player ID
        """
        self.id = pid
        self.hand = []

    def add_card(self, card1):
        """ Takes a card and adds it to the hand
        """
        self.hand.append(card1)
        return card1

    def show_hand(self):
        """ Displays player hand
        """
        print('House: ', end=' ')
        for icard in self.hand:
            print(icard.get_rank(), icard.get_suit(), end='   ')

    def total(self):
        """ Takes the hand and returns total value
        """

        values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8,
                  '9': 9, '1': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11}
        result = 0
        num_aces = 0

        for card1 in self.hand:
            result += values[card1.get_rank()[0]]       # add up the values of the cards in the hand
            if card1.get_rank()[0] == 'A':              # also add up the number of aces
                num_aces += 1

        while result > 21 and num_aces > 0:
            result -= 10
            num_aces -= 1
        return result

    def __len__(self):
        return len(self.hand)

hand = Hand('House')
deck = Deck()
deck.shuffle()
hand.add_card(deck.deal_card())
hand.add_card(deck.deal_card())
hand.add_card(deck.deal_card())

hand.show_hand()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote