I am writing a simple Yatzy app using Python, and I am a bit stuck on finding the best pair of dice. Here is the outline of the function:
Input: list containing five dice.
Output: highest sum of two dice with the same number. If no pairs found, then -1.
What would be the optimal way to write this function using Python? How can I scale it up for say two pairs or full house?
Thanks in advance.
Here's a Python3 solution that uses the collections module.
from collections import Counter
from random import randint
roll = [randint(1, 6) for x in range(5)]
result = max([x for x, cnt in Counter(roll).items() if cnt==2] or [-1])
print(roll, '->', result)
By the way, there's an edge case in here (4 of a kinds = 2 pairs), depending on your required result you may want to compare like cnt > 1
instead.