from typing import List
from typing import Any
from typing import Tuple
import itertools


def common_subsequences(
    dataset: List[Any],
    min_match_len = 3,
    max_match_len = 10) -> List[Tuple[int, List[Any]]]:
    """Catalog common sequential patterns 
    (ie, occuring twice or more) that are of length
    min_match_len to max_match_len that occur within
    list dataset.
    Return a list of (count, subsequence), most
    frequent first.
    """
    results = []

    def _count_occurrences(sublist, parent):
        return sum(parent[i:i+len(sublist)]==\
            sublist for i in range(len(parent)))

    if max_match_len > len(dataset) > 1:
        raise Exception("max_match_len cannot be > len(dataset)")

    for i1 in range(len(dataset) - max_match_len):
        previous = None
        for i2 in range(min_match_len, max_match_len):
            head = dataset[i1:i1 + i2]
            tail = dataset[i1 + i2:]
            count = _count_occurrences(head, tail)
            if head not in results:
                results = results + [head for i in range(count + 1)]

    results = [list(j) for i, j in itertools.groupby(sorted(results))]
    results = [(len(seq), seq[0]) for seq in results]
    results = filter(lambda r: r[0] > 1, results)
    results = sorted(results,
        key=lambda m: m[0], reverse=True)
    return results