In this kata, your task is to identify the pattern underlying a sequence of numbers. For example, if the sequence is [1, 2, 3, 4, 5], then the pattern is [1], since each number in the sequence is equal to the number preceding it, plus 1. See the test cases for more examples.

A few more rules :

pattern may contain negative numbers.
sequence will always be made of a whole number of repetitions of the pattern.
Your answer must correspond to the shortest form of the pattern, e.g. if the pattern is [1], then [1, 1, 1, 1] will not be considered a correct answer.

Source: codewars | Difficulty: EASY
Tags: Mathematics

Starter code: def find_pattern(seq):
	

Solutions:
from itertools import cycle

def find_pattern(s):
	diffs = [y - x for (x, y) in zip(s, s[1:])]
	for i in range(1, len(diffs) + 1):
		if len(diffs) % i == 0 and all((a == b for (a, b) in zip(diffs, cycle(diffs[:i])))):
			return diffs[:i]


def find_pattern(seq):
	(start, end) = ([], [])
	for i in range(len(seq) - 1):
		start.append(seq[i + 1] - seq[i])
		end.append(seq[-1 - i] - seq[-2 - i])
		if not (len(seq) - 1) % len(start) and start == end[::-1] and (not len(set(start)) == 1):
			return start
	return [start[0]] if len(set(start)) == 1 else []


from operator import sub

def find_pattern(sequence):
	result = list(map(sub, sequence[1:], sequence))
	size = len(result)
	for i in range(1, size + 1):
		(q, r) = divmod(size, i)
		if r == 0:
			check = result[:i]
			if check * q == result:
				return check


def find_pattern(sequence):
	diff = [b - a for (a, b) in zip(sequence, sequence[1:])]
	return next((diff[:i] for i in range(1, len(diff) + 1) if diff[i:] + diff[:i] == diff))


def find_pattern(a):
	d = [a[i + 1] - a[i] for i in range(len(a) - 1)]
	l = len(d)
	return next((d[:i] for i in range(1, len(d) + 1) if l % len(d[:i]) == 0 and d[:i] * (l // len(d[:i])) == d))


def find_pattern(a):
	diff = [a[i + 1] - a[i] for i in range(len(a) - 1)]
	l = len(diff)
	for i in range(1, len(diff) + 1):
		t = diff[:i]
		if l % len(t) == 0:
			if t * (l // len(t)) == diff:
				return t


def find_pattern(seq):
	xs = [b - a for (a, b) in zip(seq, seq[1:])]
	return next((xs[:i] for i in range(1, len(xs) // 2 + 1) if xs[:i] * (len(xs) // i) == xs), xs)


def find_pattern(sequence):
	pattern = [a - b for (a, b) in zip(sequence[1:], sequence)]
	for size in range(1, len(pattern) + 1):
		if len(pattern) // size * pattern[:size] == pattern:
			return pattern[:size]


def find_pattern(seq):
	(n, diffs) = (len(seq) - 1, [b - a for (a, b) in zip(seq, seq[1:])])
	for l in range(1, n + 1):
		if n // l * diffs[:l] == diffs:
			return diffs[:l]