codeeval_challenges / 005_DetectingCycles /

# On CodeEval, test cases are read in from a file which is the first argument to your program
# Open the file and read in line by line. Each line represents a different test case
# (unless given different instructions in the challenge description)

import sys

def detect_cycle(sequence):
    """ Detect cycles of numbers in the sequence.
        Returns None when no cycle is detected
    sequence = sequence.split()

    for sample_idx in range(len(sequence)-3):
        sample = sequence[sample_idx:sample_idx+2]

        for match_idx in range(sample_idx+2, len(sequence)-1):
            match = sequence[match_idx:match_idx+2]

            if sample == match:
                sample = sequence[sample_idx:match_idx]
                match = sequence[match_idx:match_idx+len(sample)]

                if sample == match:
                    return ' '.join(sample)
    return None

test_cases = open(sys.argv[1], 'r')

for test in test_cases:
    # ignore test if it is an empty line
    # 'test' represents the test case, do something with it
    test = test.strip()

    if test != '':
        cycle = detect_cycle(test)
        if cycle is not None:
            print cycle

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.