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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!