Answered step by step
Verified Expert Solution
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: deckofcards.py
from random import shuffle as s
class Deck:
suits Diamonds "Clubs", "Hearts", "Spades"
def initself:
cards
for s in self.suits: # Fill the deck with standard playing cards
for val in range:
cards.appendselfCards val
self.cards cards
def iterself:
return self.cards.iter
def strself: #TODO Fix this to state whether or not the deck is sorted
or shuffled;
return 'A deck of selfsize cards'.formatselfself
@property # Property to get the length of the cards list
def sizeself:
return lenselfcards
@property #TODO Implement a method to determine if the cards are sorted;
def issortedself:
pass
def sortself: #TODO Implement a method to sort cards by suit and value;
pass
def shuffleself: # Method to put cards list in random order
shuffleddeck sselfcards
return shuffleddeck
def searchself: #TODO Implement a public search method;
card self.describecard
def describecardself: # User facing private function to create a card to
search for
printWhat suit is the card?" # Pick a suit
prompt
i
for suit in self.suits: # Build prompt to pick suit
prompt
formati suit
i
while True:
s intinputprompt # Collect user info for suit
v intinputEnter a number from to Ace, Jack,
Queen, King: # Collect user info for value
if s in and v in x for x in range:
card self.Cardselfsuitss v
printcard #TODO Remove this; only here for debugging.
break
printInvalid card, try again" # If invalid try again
return card
class Card: # Private inner class to create a Card
def initself suit, value: # Need a suit and a value. Will be two
integers. for suit and for value
self.suit suit
self.value value
def strself: # Print override
return selfvaluename of selfsuitformatselfself
def eqself 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 suitnameself:
if self.suit suits:
return
elif self.suit suits:
return
elif self.suit suits:
return
elif self.suit suits:
return
else:
raise ValueError
@property # Get proper value name
def valuenameself:
if self.value :
return "Ace"
elif self.value :
return "Jack"
elif self.value :
return "Queen"
elif self.value :
return "King"
else:
return self.value
if namemain: # 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: quicksort.py bubblesort.py insertsort.py and selectionsort.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 of deckofcards.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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started