In computer science, cycle detection is the algorithmic problem of finding a cycle in a sequence of iterated function values.

For any function ƒ, and any initial value x0 in S, the sequence of iterated function values

    x0,x1=f(x0), x2=f(x1), ...,xi=f(x{i-1}),...

may eventually use the same value twice under some assumptions: S finite, f periodic ... etc. So there will be some `i ≠ j` such that `xi = xj`. Once this happens, the sequence must continue by repeating the cycle of values from `xi to xj−1`. Cycle detection is the problem of finding `i` and `j`, given `ƒ` and `x0`. Let `μ` be the smallest index such that the value associated will reappears and `λ` the smallest value such that `xμ = xλ+μ, λ` is the loop length.

Example:

Consider the sequence: 
```
2, 0, 6, 3, 1, 6, 3, 1, 6, 3, 1, ....
```

The cycle in this value sequence is 6, 3, 1.
μ is 2 (first 6)
λ is 3 (length of the sequence or difference between position of consecutive 6).

The goal of this kata is to build a function that will return `[μ,λ]` when given a short sequence. Simple loops will be sufficient. The sequence will be given in the form of an array. All array will be valid sequence associated with deterministic function. It means that the sequence will repeat itself when a value is reached a second time. (So you treat two cases: non repeating [1,2,3,4] and repeating [1,2,1,2], no hybrid cases like [1,2,1,4]). If there is no repetition you should return []. 


This kata is followed by two other cycle detection algorithms: 
Loyd's: http://www.codewars.com/kata/cycle-detection-floyds-tortoise-and-the-hare
Bret's: http://www.codewars.com/kata/cycle-detection-brents-tortoise-and-hare

Difficulty: EASY
Tags: Mathematics

Starter code:
def cycle(sequence):
	

Solutions:
def cycle(sequence):
	for (j, x) in enumerate(sequence):
		i = sequence.index(x)
		if 0 <= i < j:
			return [i, j - i]
	return []


def cycle(sequence):
	found = {}
	for (i, item) in enumerate(sequence):
		if item in found:
			return [found[item], i - found[item]]
		found[item] = i
	else:
		return []


def cycle(sequence):
	collector = []
	for s in sequence:
		if s in collector:
			mu = collector.index(s)
			return [mu, len(collector) - mu]
		collector.append(s)
	return []


def cycle(sequence):
	d = {}
	for (i, x) in enumerate(sequence):
		if x in d:
			return [d[x], i - d[x]]
		d[x] = i
	return []


def cycle(seq):
	for (i, n) in enumerate(seq):
		if n in seq[i + 1:]:
			return [i, seq.index(n, i + 1) - i]
	return []


def cycle(lst):
	return next(([i, lst.index(n, i + 1) - i] for (i, n) in enumerate(lst) if n in lst[i + 1:]), [])


def cycle(sequence):
	memo = {}
	for (i, item) in enumerate(sequence):
		if item in memo:
			return [memo[item], i - memo[item]]
		memo[item] = i
	return []


import re

def cycle(sequence):
	string = ' ' + ' '.join((str(n) for n in sequence))
	pattern = re.compile('([\\d ]+)(\\1)')
	matches = pattern.search(string)
	if matches is None:
		return []
	seq = matches.group(1)
	sequence = seq.strip().split()
	not_in = int(string.replace(seq, '') or 0)
	return [not_in, 1 if len(set(sequence)) == 1 else len(sequence)]


def cycle(seq):
	seen = dict()
	for (i, v) in enumerate(seq):
		if v in seen:
			return [seen[v], i - seen[v]]
		else:
			seen[v] = i
	return []