Skip to content

Instantly share code, notes, and snippets.

@maxwellmckinnon
Created October 31, 2017 05:05
Show Gist options
  • Save maxwellmckinnon/14047caa8ab3f2b73c613fbc666e35de to your computer and use it in GitHub Desktop.
Save maxwellmckinnon/14047caa8ab3f2b73c613fbc666e35de to your computer and use it in GitHub Desktop.
Solution to zero move nim
#!/bin/python3
# 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.
# GRUNDULER
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