Created
January 10, 2011 06:38
-
-
Save dnene/772470 to your computer and use it in GitHub Desktop.
Greed scoring in python
This file contains 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
# Rules : https://github.com/edgecase/ruby_koans/blob/master/koans/GREED_RULES.txt | |
r = ((100,1000), (0,200), (0,300), (0,400), (50,500), (0,600)) | |
def times(dct,val) : | |
dct[val-1] = dct[val-1] + 1 | |
return dct | |
def score(dice) : | |
dct = reduce(times,dice,dict((x,0) for x in range(6))) | |
return reduce(lambda s, (t,c) : r[t][0] * (c % 3) + r[t][1] * (c / 3) + s, | |
dct.items(),0) | |
if __name__ == "__main__" : | |
assert score((5,1,3,4,1)) == 250 | |
assert score((1,1,1,3,1)) == 1100 | |
assert score((2,4,4,5,4)) == 450 |
Ha ha, .. truth be told I wrote it a little verbose and then tersified it. Though I think this structure (if translated to clojure) might actually work out quite nicely. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
jumm too much terse?? :-P