Skip to content

Instantly share code, notes, and snippets.

@jarodl
Created March 30, 2012 02:13
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 jarodl/2245817 to your computer and use it in GitHub Desktop.
Save jarodl/2245817 to your computer and use it in GitHub Desktop.
"""
Detecting cycles
----------
Solution for the cycles challenge.
"""
def read_input(input_filename="input.txt"):
with open(input_filename) as f:
return (line.rstrip().split() for line in f.readlines())
def find_cycles(number_set):
size = len(number_set)
max_len = (size / 2) + 1
for i in range(size):
for j in range(max_len):
curr = number_set[i:i + j]
next = number_set[i + j:(i + j) + j]
if curr == next and curr and next:
yield curr
def main():
number_sets = read_input("cycles.txt")
for number_set in number_sets:
cycles = [str.join(' ', cycle) for cycle in find_cycles(number_set)]
if cycles:
print cycles
if __name__ == '__main__':
main()
2 0 6 3 1 6 3 1 6 3 1
0 6 3 1 6 3 1 6 3 1 2
1 2 3 4 5 6 7 8 9
6 6
1 6 1 6
1 6 6
1 2 3 4 5 6 7 8 9 10 11 12 11 12
1 2 1 2
9 9 9 8
1 2 1 2 3 4 3 4 5 6 5 6 7 8 7 8
1 2 3 4 5 6 5 6 7 8 7 8 9 0
1 2 0 1 2
7 8 9 7 1 2 4 5 6 5 6
7 8 9 7 1 2 4 5 6 7 5 6 7
7 8 9 7 1 2 4 7 5 6 7 5 6 7 6 5 3
['6 3 1', '3 1 6', '1 6 3', '6 3 1']
['6 3 1', '3 1 6', '1 6 3', '6 3 1']
['6']
['1 6']
['6']
['11 12']
['1 2']
['9', '9']
['1 2', '3 4', '5 6', '7 8']
['5 6', '7 8']
['5 6']
['5 6 7']
['7 5 6', '5 6 7']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment