You are viewing a single comment's thread. Return to all comments →
easy recursive solution:
def is_pow2(n): return n > 0 and (n & (n-1)) == 0 def helper(n, state): nxt_state = (state + 1) % 2 if n == 1: return nxt_state if n < 4: return state if is_pow2(n): nxt_n = n // 2 return helper(nxt_n, nxt_state) else: bn = n.bit_length() nxt_n = n ^ (1 << (bn-1)) return helper(nxt_n, nxt_state) def counterGame(n): # Write your code here ''' 1 richard 2 louise 3 louise 4 richard 0-l, 1-r ''' return 'Richard' if helper(n, 0) else 'Louise'
Seems like cookies are disabled on this browser, please enable them to open this website
Counter game
You are viewing a single comment's thread. Return to all comments →
easy recursive solution: