-1

I'm trying to create a program that allows users to play poker (for fake money, don't come after me IRS), and had a problem trying to determine kickers for better hands. So initially my thought was to sort the values of the cards (without suits) which I have like [14, 12, 10, 8, 6] (with 14 for ace high). And compare it to another list, say [14, 12, 10, 8, 7] (7 kicker) to see which ranks higher. Each hand comes from one of the 5 card combinations from the 7 available cards in a hold 'em game. My initial thought was to do:

def better_hand_with_kicker(list0, list1):
    for index in range(5):
        if list0[index] >= list1[index]:
            continue
        else:
            return False
    return True

Yet, this won't work if any number was smaller. For instance between [14, 12, 10, 8, 6] and [14, 13, 9, 7, 5] it will return that [14, 12, 10, 8, 6] is superior because even though (13 > 12) makes the other hand win, the third slot of (9 < 10) will trigger the return False clause.

Therefore I thought of a slightly more complex:

def better_hand_with_kicker(list0, list1):
    for index in range(5):
        if list0[index] == list1[index]:
            continue
        elif list0[index] > list1[index]:
            return True
        else:
            return False
    return True # this would really be a tie

Now this works, but I figure there must be a much more elegant solution...

Reedinationer
  • 5,661
  • 1
  • 12
  • 33

1 Answers1

1

There was a much more elegant solution! After searching the web for ideas about inequalities between lists and finding this amazing post I realized that my solution was incredibly inadequate. The best solution would be to use:

def better_hand_with_kicker(list0, list1):
    if list0 > list1:
        return True
    return False

This uses the fact that an inequality of a list will evaluate similarly to the other function I created, specifically:

The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.

The caveat is that each list must be sorted() prior to this inequality evaluation. Hopefully this post helps somebody in the future trying to use inequalities on lists of integers. In my use-case this is so much more condensed, it turned an entire function I was writing into a single if statement that I no longer truly need a function for! Now my code can simply be:

if list0 > list1:
    # do stuff

With no function necessary. I'm not sure why online tutorials don't seem to cover this feature...Anyways,

Praise be to StackOverflow!

Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • 1
    This can be simplified to `return list0 > list1`. – iz_ May 10 '19 at 23:52
  • @Tomothy32 Yes, that's what I meant by " it turned an entire function I was writing into a single `if` statement. I edited it to be more clear now, thank you for the comment! – Reedinationer May 11 '19 at 02:35