Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Solution to zero move nim
# Chris Grundy McKinnon method
import sys
from functools import reduce
g = int(input().strip())
for a0 in range(g):
n = int(input().strip()) # num of heaps
p = [int(p_temp) for p_temp in input().strip().split(' ')] # pile counts
# your code goes here
# p is already the nimber of each pile.
p = [(pt+1 if pt%2 == 1 else pt-1) for pt in p]
nimsum = reduce((lambda x,y: x^y), p)
won = False if nimsum == 0 else True
print('W') if won else print('L')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment