Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Modify __cmp__ so that Aces are ranked higher than Kings. def __cmp__(

ID: 3619935 • Letter: Q

Question

Question:
Modify __cmp__ so that Aces are ranked higher than Kings.

def __cmp__(self, other):
   # check the suits
   if self.suit > other.suit: return 1
   if self.suit < other.suit: return -1
   # suits are the same... check ranks
   if self.rank > other.rank: return 1
   if self.rank < other.rank: return -1
   # ranks are the same... it’s a tie
   return 0

In this ordering, Aces appear lower than Deuces (2s). This code is from a card game

This exercise comes from Chapter 15.4 from ThinkCspy book which can be found here
http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap15.html
--
The reading material for the above question is posted below.
---

Comparing cards:

For primitive types, there are conditional operators (<, >, ==, etc.) that compare
values and determine when one is greater than, less than, or equal to another.
For user-defined types, we can override the behavior of the built-in operators by
providing a method named cmp . By convention, cmp has two parameters,
self and other, and returns 1 if the first object is greater, -1 if the second object
is greater, and 0 if they are equal to each other.

Some types are completely ordered, which means that you can compare any two
elements and tell which is bigger. For example, the integers and the floating-point
numbers are completely ordered. Some sets are unordered, which means that
there is no meaningful way to say that one element is bigger than another. For
example, the fruits are unordered, which is why you cannot compare apples and
oranges.

The set of playing cards is partially ordered, which means that sometimes you can
compare cards and sometimes not. For example, you know that the 3 of Clubs is
higher than the 2 of Clubs, and the 3 of Diamonds is higher than the 3 of Clubs.
But which is better, the 3 of Clubs or the 2 of Diamonds? One has a higher rank,
but the other has a higher suit.

In order to make cards comparable, you have to decide which is more important,
rank or suit. To be honest, the choice is arbitrary. For the sake of choosing, we
will say that suit is more important, because a new deck of cards comes sorted
with all the Clubs together, followed by all the Diamonds, and so on.

With that decided, we can write cmp :

def __cmp__(self, other):
   # check the suits
   if self.suit > other.suit: return 1
   if self.suit < other.suit: return -1
   # suits are the same... check ranks
   if self.rank > other.rank: return 1
   if self.rank < other.rank: return -1
   # ranks are the same... it’s a tie
   return 0

In this ordering, Aces appear lower than Deuces (2s).

All of chapter 15 can be found here.
http://www.greenteapress.com/thinkpython/thinkCSpy/html/chap15.html
In this ordering, Aces appear lower than Deuces (2s).

Explanation / Answer

There are fifty-two cards in a deck, each of which belongs to one of four suits and one of thirteen ranks. The suits are Spades, Hearts, Diamonds, and Clubs (in descending order in bridge). The ranks are Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. Depending on the game that you are playing, an Ace may be higher than King or lower than 2.

The modified _cmp_ function is

# inside class Card:

    def __cmp__(self, other):

        T1 = self.suit, self.rank

        T2 = other.suit, other.rank

        return cmp(T1, T2)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote