Skip to content

Instantly share code, notes, and snippets.

@davidrobles
Created March 7, 2017 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrobles/d9b25a84a5693872a7f84af154c43f1b to your computer and use it in GitHub Desktop.
Save davidrobles/d9b25a84a5693872a7f84af154c43f1b to your computer and use it in GitHub Desktop.
def cycle(a):
n = len(a)
idx = 0
if a[idx] == 0:
return len(a) == 1
for i in range(n):
idx = (idx + a[idx]) % n
return idx == 0
assert cycle([0]) == True
assert cycle([0, 0]) == False
assert cycle([0, 1]) == False
assert cycle([1, 0]) == False
assert cycle([1, 1]) == True
assert cycle([1, -1]) == True
assert cycle([2, 2, 2]) == True
assert cycle([2, 2, 1]) == False
assert cycle([1, 2, 0]) == False
assert cycle([2, 2, 0]) == False
assert cycle([2, 2, -1]) == True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment