def moving_permutation(length, origin, goal):
    """
    Returns a permutation moving the element at position origin to the position goal (in the format requested by
    torch.Tensor.permute)

    Parameters
    ----------
    length : int
        length of the sequence to be permuted
    origin : int
        position of the element to be moved
    goal : int
        position the element should end up after the permutation

    Returns
    -------
    :obj:`list` of :obj:`int`

    Examples
    --------

    >>> moving_permutation(length=5, origin=1, goal=3)
    [0, 2, 3, 1, 4]
    >>> moving_permutation(length=5, origin=3, goal=1)
    [0, 3, 1, 2, 4]

    """
    result = []
    for i in range(length):
        if i == goal:
            result.append(origin)
        elif (i < goal and i < origin) or (i > goal and i > origin):
            result.append(i)
        elif goal < i <= origin:
            result.append(i-1)
        elif origin <= i < goal:
            result.append(i+1)
        else:
            assert False
    return result