def perm(a):
    if len(a) <= 1:
        return [a]

    ret = []
    for x in a:
        tmp = a[:]
        tmp.remove(x)
        for p in perm(tmp):
            p.insert(0, x)
            ret.append(p)

    return ret


def var(a, k, rep=False):
    if k == 0:
        return [[]]

    ret = []
    for x in a:
        tmp = a[:]
        if not rep:
            tmp.remove(x)
        for v in var(tmp, k-1, rep):
            v.insert(0, x)
            ret.append(v)

    return ret


def komb(a, k, rep=False):
    if rep:
        out = [[b] for b in a]
        for _ in xrange(1, k):
            out = [b + [c] for b in out for c in a]
        return out

    if k == 0:
        return[[]]
    if len(a) < k:
        return []

    out = []
    x = a.pop()
    for ko in komb(a, k-1):
        ko.append(x)
        out.append(ko)
    out.extend(komb(a, k))
    a.append(x)

    return out

a = [2, 3, 4, 5]
k = 2
print "Permutacie: \n", perm(a)
print "\nKombinacie: \n", komb(a, k)
print "\nKombinacie s opakovanim: \n", komb(a, k, True)
print "\nVariacie: \n", var(a, k)
print "\nVariacie s opakovanim: \n", var(a, k, True)

