Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Generator solution in Uncategorized category for Unfair Dice by Sim0000
def winning_die(enemy_die):
def judge(p, e):
return sum((xp > xe) - (xp < xe) for xp in p for xe in e)
# generator for partition with length n
def g(n, remain, last=1):
if n == 1:
if remain <= 18: yield [remain]
else:
yield from [[i]+x for i in range(last, remain//n+1) for x in g(n-1, remain-i, i)]
for player_die in g(len(enemy_die), sum(enemy_die)):
if judge(player_die, enemy_die) > 0: return player_die
return []
if __name__ == '__main__':
#These are only used for self-checking and not necessary for auto-testing
def check_solution(func, enemy):
player = func(enemy)
total = 0
for p in player:
for e in enemy:
if p > e:
total += 1
elif p < e:
total -= 1
return total > 0
assert check_solution(winning_die, [3, 3, 3, 3, 6, 6]), "Threes and Sixes"
assert check_solution(winning_die, [4, 4, 4, 4, 4, 4]), "All Fours"
assert check_solution(winning_die, [1, 1, 1, 4]), "Unities and Four"
assert winning_die([1, 2, 3, 4, 5, 6]) == [], "All in row -- No die"
June 14, 2014
Comments: