Created
March 30, 2012 02:13
-
-
Save jarodl/2245817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['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