Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Stones by tigerhu3180
def stones(pile, moves):
results = [True] + [False] * pile
for i in range(1, pile + 1):
leave = [0 if i - j <= 0 else i - j for j in moves]
if all([results[s] for s in leave]): # for the next player
#The current player loses if all of the results for the next player are True.
#The current player can win if there is at least one choice that is False.
results[i] = False # for the current player
else:
results[i] = True # for the current player
return 1 if results[pile] == True else 2
if __name__ == '__main__':
print("Example:")
print(stones(17, [1, 3, 4]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert stones(17, [1, 3, 4]) == 2
assert stones(17, [1, 3, 4, 6, 9]) == 1
assert stones(99, [1]) == 2
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 27, 2018
Comments: