Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You have been provided a code for creating a deck of cards: deck _ of _ cards.py from random import shuffle as s class Deck:

You have been provided a code for creating a deck of cards: deck_of_cards.py
from random import shuffle as s
class Deck:
_suits =["Diamonds", "Clubs", "Hearts", "Spades"]
def __init__(self):
cards =[]
for s in self._suits: # Fill the deck with standard playing cards
for val in range(1,14):
cards.append(self._Card(s, val))
self.cards = cards
def __iter__(self):
return self.cards.__iter__()
def __str__(self): #TODO Fix this to state whether or not the deck is sorted
or shuffled;
return 'A deck of {self.size} cards'.format(self=self)
@property # Property to get the length of the cards list
def size(self):
return len(self.cards)
@property #TODO Implement a method to determine if the cards are sorted;
def is_sorted(self):
pass
def sort(self): #TODO Implement a method to sort cards by suit and value;
pass
def shuffle(self): # Method to put cards list in random order
shuffled_deck = s(self.cards)
return shuffled_deck
def search(self): #TODO Implement a public search method;
card = self._describe_card()
def _describe_card(self): # User facing private function to create a card to
search for
print("What suit is the card?") # Pick a suit
prompt =""
i =1
for suit in self._suits: # Build prompt to pick suit
prompt +='{}.{}
'.format(i, suit)
i +=1
while True:
s = int(input(prompt)) # Collect user info for suit
v = int(input("Enter a number from 1 to 13(1= Ace, 11= Jack, 12=
Queen, 13= King): ")) # Collect user info for value
if s in [1,2,3,4] and v in [x for x in range(1,14)]:
card = self._Card(self._suits[s -1], v)
print(card) #TODO Remove this; only here for debugging.
break
print("Invalid card, try again") # If invalid try again
return card
class _Card: # Private inner class to create a Card
def __init__(self, suit, value): # Need a suit and a value. Will be two
integers. 0-3 for suit and 1-13 for value
self.suit = suit
self.value = value
def __str__(self): # Print override
return '{self.value_name} of {self.suit}'.format(self=self)
def __eq__(self, card): # Equals override
if self.suit != card.suit:
return False
if self.value != card.value:
return False
return True
@property # Get proper suit name
def suit_name(self):
if self.suit ==_suits[0]:
return 0
elif self.suit ==_suits[1]:
return 1
elif self.suit ==_suits[2]:
return 2
elif self.suit ==_suits[3]:
return 3
else:
raise ValueError()
@property # Get proper value name
def value_name(self):
if self.value ==1:
return "Ace"
elif self.value ==11:
return "Jack"
elif self.value ==12:
return "Queen"
elif self.value ==13:
return "King"
else:
return self.value
if __name__=='__main__': # Main method
deck = Deck() # Create empty Deck object
deck.shuffle()
deck.search()
In addition you have been given a few different codes for sorting elements in a list: quick_sort.py, bubble_sort.py, insert_sort.py, and selection_sort.py. What you have been
tasked to do is:
Utilize one of the above sorting algorithms to sort the cards in the deck
Both by suit and by value.
The suits should be ordered in the same order in which the deck is created (see line 6 of deck_of_cards.py)
Create a property that determines whether or not the deck is sorted.
Create a search method that allows a user to describe a card and will return the location (array index) of the card.
This search can easily be made to be high intelligent
What can you do to make this search a constant time look up?
Finish all other leftover TODO's in the code as described.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions