Question
Bubble Sort Cards In this assignment, you will create a function, sort_cards(), that takes one required argument, a list of cards, and an optional argument,
Bubble Sort Cards
In this assignment, you will create a function, sort_cards(), that takes one required argument, a list of cards, and an optional argument, ace_high, which defaults to True. The function will return the cards sorted lowest to highest by rank, ignoring the suit. If ace_high is True, aces should be considered the highest rank. If ace_high is False, they should be considered the lowest. The algorithm should also be stable; if the Queen of Hearts comes before the Queen of Clubs in the unsorted list, it should be the same in the sorted list.
I recommend that you create a helper function to compare two cards and return a boolean telling you whether the cards are in order or not. This will allow you to easily test this part of the sorting algorithm. You can design it so you pass in the value of ace_high so you can encapsulate this aspect of the problem in one place. Additionally, this function will be of help in a future problem.
Side notes:
-
The method in the Card class called __repr__()is called when you print out a card object. This is why it prints things like "2 of Spades" and "Ace of Hearts" instead of Card Object at HEX.
-
The other odd method in the card class called __eq__()defines a way to check equality between cards. By defining this, Card(5) == Card(5)will return Truesince their attributes (suit and rank) are the same. Without this, Card(5) == Card(5)would return Falsesince it would check if they were literally the same instance of the object.
Example output:
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