Created
October 7, 2011 14:52
-
-
Save flopezluis/1270433 to your computer and use it in GitHub Desktop.
Yahtzee categories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import imap, groupby | |
def get_duplicates(sec): | |
return [x for x in sec if sec.count(x) > 1] | |
class SumEquals(object): | |
""" | |
Ones, Twos, Threes, Fours, Fives, Sixes: | |
""" | |
def __init__(self, number): | |
self.number = number | |
def solve(self, sec): | |
""" | |
>>> se = SumEquals(1) | |
>>> se.solve([2,2,1,3,3]) | |
1 | |
>>> se = SumEquals(3) | |
>>> se.solve([1,3,1,3,3]) | |
9 | |
>>> se = SumEquals(6) | |
>>> se.solve([1,2,3,4,5]) | |
0 | |
""" | |
return sum([x for x in sec if x == self.number]) | |
class Pair(object): | |
""" | |
Pair | |
""" | |
def solve(self, sec): | |
""" | |
>>> pair = Pair() | |
>>> pair.solve([2,2,1,3,3]) | |
6 | |
>>> pair.solve([1,2,1,4,3]) | |
2 | |
>>> pair.solve([1,2,3,4,5]) | |
0 | |
""" | |
duplicates = get_duplicates(sec) | |
if duplicates: | |
return 2*max(duplicates) | |
return 0 | |
class TwoPairs(object): | |
""" | |
Two pairs | |
""" | |
def solve(self, sec): | |
""" | |
>>> tw = TwoPairs() | |
>>> tw.solve([2,2,1,3,3]) | |
10 | |
>>> tw.solve([1,2,1,3,3]) | |
8 | |
>>> tw.solve([1,2,3,4,5]) | |
0 | |
""" | |
duplicates = get_duplicates(sec) | |
if duplicates > 1: | |
return sum(duplicates) | |
return 0 | |
class SameKind(object): | |
""" | |
Three of a kind | |
Four of a kind | |
""" | |
def __init__(self, kind): | |
assert kind == 3 or kind == 4, "Allowed values are 3 or 4" | |
self.kind = kind | |
def solve(self, sec): | |
""" | |
>>> sk = SameKind(3) | |
>>> sk.solve([2,2,2,3,3]) | |
6 | |
>>> sk.solve([1,2,1,3,3]) | |
0 | |
>>> sk = SameKind(4) | |
>>> sk.solve([4,5,4,4,4]) | |
16 | |
""" | |
return sum([x for x in sec if sec.count(x) == self.kind]) | |
class SmallStraight(object): | |
""" | |
SmallStraigh | |
""" | |
def solve(self, sec): | |
""" | |
>>> st = SmallStraight() | |
>>> st.solve([2,2,2,3,3]) | |
0 | |
>>> st.solve([1,2,3,4,3]) | |
10 | |
>>> st.solve([1,2,2,4,5]) | |
0 | |
""" | |
if all(imap(lambda x,y:x==y, range(1,5), sec)): | |
return sum(sec[:-1]) | |
return 0 | |
class LargeStraight(object): | |
""" | |
LargeStraigh | |
""" | |
def solve(self, sec): | |
""" | |
>>> lt = LargeStraight() | |
>>> lt.solve([2,2,2,3,3]) | |
0 | |
>>> lt.solve([2,3,4,5,6]) | |
20 | |
>>> lt.solve([2,3,4,5,4]) | |
0 | |
""" | |
if all(imap(lambda x,y:x==y, range(2,7), sec)): | |
return sum(sec) | |
return 0 | |
class FullHouse(object): | |
""" | |
LargeStraigh | |
""" | |
def solve(self, sec): | |
""" | |
>>> fh = FullHouse() | |
>>> fh.solve([2,2,2,3,3]) | |
12 | |
>>> fh.solve([1,1,2,2,6]) | |
0 | |
>>> fh.solve([2,2,3,3,3]) | |
13 | |
>>> fh.solve([2,3,3,3,3]) | |
0 | |
""" | |
groups = {} | |
for key, group in groupby(sorted(sec)): | |
groups[len(list(group))] = 1 | |
if groups.has_key(2) and groups.has_key(3): #there must be only one group of 3 and other of 2 | |
return sum(sec) | |
return 0 | |
class Yahtzee(object): | |
""" | |
Yahtzee | |
""" | |
def solve(self, sec): | |
""" | |
>>> ya = Yahtzee() | |
>>> ya.solve([2,2,2,3,3]) | |
0 | |
>>> ya.solve([1,1,1,1,1]) | |
50 | |
>>> ya.solve([3,3,3,3,3]) | |
50 | |
>>> ya.solve([2,3,3,3,3]) | |
0 | |
""" | |
if sec.count(sec[0]) == 5: | |
return 50 | |
return 0 | |
class Chance(object): | |
""" | |
Chance | |
""" | |
def solve(self, sec): | |
""" | |
>>> ch = Chance() | |
>>> ch.solve([2,1,2,3,3]) | |
11 | |
>>> ch.solve([1,2,3,4,5,6]) | |
21 | |
>>> ch.solve([3,3,3,3,3]) | |
15 | |
>>> ch.solve([2,3,3,3,3]) | |
14 | |
""" | |
return sum(sec) | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment