The poker ranks go in order like this. '23456789TJQKA' where T is ten and then jack queen king and Ace with Ace being the highest card. A string is inputed and is always inputed as a 5 character string.
If the user inputs lets say string = '43TKQ' How can I find the highest card in this string without any advanced methods only if statements, while and for loops. I tried max(string) but it returns ten and not King
hand = '28TKQ'
print(max(hand)) #This outputs T (it should output K)
'768TA' should print A
'2TJ67' should print J
You need to specify to Order logic witch is 23456789TJQKA
:
order = '23456789TJQKA'
print(max('28TKQ', key=lambda e: order.index(e))) # K
print(max('A43TKQ', key=lambda e: order.index(e))) # A
Another pythonic code:
cards = '23456789TJQKA'
print(max('28TKQ', key=cards.index)) # K
print(max('A43TKQ', key=cards.index)) # A
EDITS
To answer your comment:
when you search for max
in list element you will compare items, by default str
are compared by Unicode code point
, so when you compare element of list 28TKQ
'T'
have the biggest value 84
, ord('T') > ord('K')
and this why max
returned T
not K
, but you don't what this logic of compression you need to tell max
that you want a different logic in comparing.
this why we use the key
parameter that we pass a method and the item are ordered based on the return value of that method.
Edits:
To clear thing for you more
# 2 priority is 0, ....., A priority is 12
card_priority = '23456789TJQKA'
def get_card_priority(card):
""" return the priority of card, it's index in card_priority list"""
return card_priority.index(card)
# compare get_card_priority('2') = 0, get_card_priority('8') = 6,get_card_priority('T')=8.......get_card_priority('Q') = 10
# The element with the highest value is returned witch is 'K' = 11
print(max('28TKQ', key=get_card_priority)) # K because get_card_priority('K') return the biggest value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With