I'm currently stuck on an assignment, where it asks to build a function and rank the suits in a certain order (♢,♣,♡,♠)
My code should end up returning this:
['10♢', '10♣', '10♡', '10♠', '2♢', '2♣', '2♡', '2♠', '3♢', '3♣', '3♡', '3♠', '4♢', '4♣', '4♡', '4♠', '5♢', '5♣', '5♡', '5♠', '6♢', '6♣', '6♡', '6♠', '7♢', '7♣', '7♡', '7♠', '8♢', '8♣', '8♡', '8♠', '9♢', '9♣', '9♡', '9♠', 'A♢', 'A♣', 'A♡', 'A♠', 'J♢', 'J♣', 'J♡', 'J♠', 'K♢', 'K♣', 'K♡', 'K♠', 'Q♢', 'Q♣', 'Q♡', 'Q♠']
Here is my code:
def helper_function(cards):
value = 0
for string in cards:
if '♢' in string:
value = 1
if '♣' in string:
value = 2
if '♡' in string:
value = 3
if '♠' in string:
value = 4
def card_sorter_v1(cards):
return sorted(cards, key = helper_function)
I know I need to use custom comparisons, but I am not sure what else to put in the helper function to sort this deck. I cannot hard-code any values because the testing framework will include edge cases. Any tips or advice?
Here is the output I am currently receiving:
['A♢', '2♢', '3♢', '4♢', '5♢', '6♢', '7♢', '8♢', '9♢', '10♢', 'J♢', 'Q♢', 'K♢', 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♡', '2♡', '3♡', '4♡', '5♡', '6♡', '7♡', '8♡', '9♡', '10♡', 'J♡', 'Q♡', 'K♡', '15♡', 'A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠']