Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python. Modify the Deck class to keep track of the current size of the deck using an instance variable. from random import randrange from Card

Python. Modify the Deck class to keep track of the current size of the deck using an instance variable.

from random import randrange from Card import Card class Deck(object):

#------------------------------------------------------------

def __init__(self):

"""post: Creates a 52 card deck in standard order"""

cards = [] self.size = 0 for suit in Card.SUITS: for rank in Card.RANKS: cards.append(Card(rank,suit)) self.cards = cards

#------------------------------------------------------------

def size(self):

"""Cards left post: Returns the number of cards in self"""

return len(self.cards)

#------------------------------------------------------------

def deal(self):

"""Deal a single card pre: self.size() > 0 post: Returns the next card, and removes it from self.card if the deck is not empty, otherwise returns False"""

if self.size() > 0: return self.cards.pop() else: return False

#------------------------------------------------------------

def shuffle(self):

"""Shuffles the deck post: randomizes the order of cards in self"""

n = self.size() cards = self.cards for i,card in enumerate(cards): pos = randrange(i,n) cards[i] = cards[pos] cards[pos] = card

#------------------------------------------------------------

def addTop(self, card): self.cards = self.cards + [card] # add [card] to the top position

#------------------------------------------------------------

def addBottom(self, card): self.cards = [card] + self.cards # add [card] to the bottom position

#------------------------------------------------------------ def addRandom(self, card): pos = randrange(range(len(self.cards))) # randomizes range of len(self.cards) self.cards = self.cards[1: pos] + [card] + self.cards[pos:] # Adding card at a random position

image text in transcribed

image text in transcribed

from random import randrange from Card import Card class Deck (object) def -init-(self): " " "post: Creates a 52 card deck in standard order""" cards - [] self. size for suit in Card.SUITS: for rank in Card. RANKS cards.append (Card (rank, suit)) self.cardscards def size (self): ""Cards left post: Returns the number of cards in self""" return len (self.cards) def deal (self) " "Deal a single card pre: self.size) o post: Returns the next card, and removes it from self.card if the deck is not empty, otherwise returns False""" if self.size) >0: return self.cards.pop () else: return False def shuffle (self): """Shuffles the deck post: randomizes the order of cards in self""" n - self.size) cards-self.cards for i,card in enumerate (cards) posrandrange (i,n) cards [i] cards [pos] cards Ipos]card def addTop (self, card) self.cards-self.cards + [card] # add [card] to the top position def addBottom (self, card) self.cards- [card] + self.cards # add [card] to the bottom position def addRandom (self, card) pos = randrange (range (len(self.cards))) # randomizes range of len(self. cards) self.cards self. cards [1: pos] + [card] + self.cards [pos:] # Adding card at a random position

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Nested Relations And Complex Objects In Databases Lncs 361

Authors: Serge Abiteboul ,Patrick C. Fischer ,Hans-Jorg Schek

1st Edition

3540511717, 978-3540511717

More Books

Students also viewed these Databases questions

Question

Define procedural justice. How does that relate to unions?

Answered: 1 week ago