def distinct_permutations(T):
    """
    Given a tuple with possibly repeated elements return the distinct permutations
    """
    
    def should_swap(L, start, cur):
        for i in range(start, cur):
            if L[i] == L[cur]:
                return False
        return True
    
    def distinct_recur(L,pos):
        if pos == len(L)-1:
            yield tuple(L)
        else:
            for i in range(pos, len(L)):
                if should_swap(L, pos, i):
                    L[pos], L[i] = L[i], L[pos]
                    yield from distinct_recur(L, pos + 1)
                    L[pos], L[i] = L[i], L[pos]
    
    yield from distinct_recur(list(T),0)