107 lines
2.1 KiB
Python
107 lines
2.1 KiB
Python
"""
|
||
This exercise stub and the test suite contain several enumerated constants.
|
||
|
||
Since Python 2 does not have the enum module, the idiomatic way to write
|
||
enumerated constants has traditionally been a NAME assigned to an arbitrary,
|
||
but unique value. An integer is traditionally used because it’s memory
|
||
efficient.
|
||
It is a common practice to export both constants and functions that work with
|
||
those constants (ex. the constants in the os, subprocess and re modules).
|
||
|
||
You can learn more here: https://en.wikipedia.org/wiki/Enumerated_type
|
||
"""
|
||
|
||
from collections import Counter
|
||
|
||
# Score categories.
|
||
# Change the values as you see fit.
|
||
YACHT = 0
|
||
ONES = 1
|
||
TWOS = 2
|
||
THREES = 3
|
||
FOURS = 4
|
||
FIVES = 5
|
||
SIXES = 6
|
||
FULL_HOUSE = 7
|
||
FOUR_OF_A_KIND = 8
|
||
LITTLE_STRAIGHT = 9
|
||
BIG_STRAIGHT = 10
|
||
CHOICE = 11
|
||
|
||
|
||
def _yacht(dice):
|
||
return 50*all(x == dice[0] for x in dice)
|
||
|
||
|
||
def _ones(dice):
|
||
return Counter(dice)[1]
|
||
|
||
|
||
def _twos(dice):
|
||
return 2*Counter(dice)[2]
|
||
|
||
|
||
def _threes(dice):
|
||
return 3*Counter(dice)[3]
|
||
|
||
|
||
def _fours(dice):
|
||
return 4*Counter(dice)[4]
|
||
|
||
|
||
def _fives(dice):
|
||
return 5*Counter(dice)[5]
|
||
|
||
|
||
def _sixes(dice):
|
||
return 6*Counter(dice)[6]
|
||
|
||
|
||
def _full_house(dice):
|
||
c = Counter(dice)
|
||
|
||
if sorted(list(c.values())) != [2, 3]:
|
||
return 0
|
||
|
||
return sum(k*v for k, v in c.items())
|
||
|
||
|
||
def _four_of_a_kind(dice):
|
||
|
||
c = Counter(dice)
|
||
keys = list(c.keys())
|
||
if c[keys[0]] >= 4:
|
||
return keys[0]*4
|
||
if c[keys[1]] >= 4:
|
||
return keys[1]*4
|
||
return 0
|
||
|
||
|
||
def _little_straight(dice):
|
||
return 30*(sorted(dice) == [1, 2, 3, 4, 5])
|
||
|
||
|
||
def _big_straight(dice):
|
||
return 30*(sorted(dice) == [2, 3, 4, 5, 6])
|
||
|
||
|
||
def _choice(dice):
|
||
return sum(dice)
|
||
|
||
|
||
_CATEGORIES = {YACHT: _yacht,
|
||
ONES: _ones,
|
||
TWOS: _twos,
|
||
THREES: _threes,
|
||
FOURS: _fours,
|
||
FIVES: _fives,
|
||
SIXES: _sixes,
|
||
FULL_HOUSE: _full_house,
|
||
FOUR_OF_A_KIND: _four_of_a_kind,
|
||
LITTLE_STRAIGHT: _little_straight,
|
||
BIG_STRAIGHT: _big_straight,
|
||
CHOICE: _choice}
|
||
|
||
|
||
def score(dice, category):
|
||
return _CATEGORIES[category](dice)
|