Python Koans is an interactive tutorial for learning the Python programming language by making tests pass.
— https://github.com/gregmalcolm/python_koans
- About Scoring Project - Greed Dice Game
Python Koans is an interactive tutorial for learning the Python programming language by making tests pass.
— https://github.com/gregmalcolm/python_koans
# -*- coding: utf-8 -*- | |
# Greed is a dice game where you roll up to five dice to accumulate | |
# points. The following "score" function will be used calculate the | |
# score of a single roll of the dice. | |
# | |
# A greed roll is scored as follows: | |
# | |
# * A set of three ones is 1000 points | |
# | |
# * A set of three numbers (other than ones) is worth 100 times the | |
# number. (e.g. three fives is 500 points). | |
# | |
# * A one (that is not part of a set of three) is worth 100 points. | |
# | |
# * A five (that is not part of a set of three) is worth 50 points. | |
# | |
# * Everything else is worth 0 points. | |
# | |
# | |
# Examples: | |
# | |
# score([1,1,1,5,1]) => 1150 points | |
# score([2,3,4,6,2]) => 0 points | |
# score([3,4,5,3,3]) => 350 points | |
# score([1,5,1,2,4]) => 250 points | |
def score(dice): | |
counts = {} | |
for die in dice: | |
counts[f"_{die}"] = counts.get(f"_{die}", 0) + 1 | |
return (lambda _1=0, _2=0, _3=0, _4=0, _5=0, _6=0: ( | |
_1 // 3 * 1e3 + _1 % 3 * 1e2 + _5 % 3 * 50 + | |
( | |
_2 // 3 * 2 + | |
_3 // 3 * 3 + | |
_4 // 3 * 4 + | |
_5 // 3 * 5 + | |
_6 // 3 * 6 | |
) * 1e2 | |
))(**counts) |
# -*- coding: utf-8 -*- | |
# Greed is a dice game where you roll up to five dice to accumulate | |
# points. The following "score" function will be used calculate the | |
# score of a single roll of the dice. | |
# | |
# A greed roll is scored as follows: | |
# | |
# * A set of three ones is 1000 points | |
# | |
# * A set of three numbers (other than ones) is worth 100 times the | |
# number. (e.g. three fives is 500 points). | |
# | |
# * A one (that is not part of a set of three) is worth 100 points. | |
# | |
# * A five (that is not part of a set of three) is worth 50 points. | |
# | |
# * Everything else is worth 0 points. | |
# | |
# | |
# Examples: | |
# | |
# score([1,1,1,5,1]) => 1150 points | |
# score([2,3,4,6,2]) => 0 points | |
# score([3,4,5,3,3]) => 350 points | |
# score([1,5,1,2,4]) => 250 points | |
def score(dice): | |
# You need to write this method | |
dice_counts = Counter(dice) | |
total_score = 0 | |
SET_MAX_VAL = 3 # A set is 3 items | |
for die, count in dice_counts.items(): | |
value = 1000 if die is 1 else die * 100 | |
total_score += value * (count // SET_MAX_VAL) | |
total_score += (dice_counts.get(1, 0) % 3) * 100 | |
total_score += (dice_counts.get(5, 0) % 3) * 50 | |
return total_score |