Skip to content

Instantly share code, notes, and snippets.

@cjmcmurtrie
Created September 3, 2016 16:02
Show Gist options
  • Save cjmcmurtrie/b68b66fd03758c3b3de2a9856dbc73b0 to your computer and use it in GitHub Desktop.
Save cjmcmurtrie/b68b66fd03758c3b3de2a9856dbc73b0 to your computer and use it in GitHub Desktop.
def distance_permutation(array, d):
"""
This function returns a staggered permutation of a sequence an the corresponding
permuted indices, such that no item is allowed to move more than d steps from
it's original position.
"""
rng = range(len(array))
for i in rng:
swap = randint(0, d)
if i - swap < rng[i] < i + swap:
rng[i], rng[min(len(array)-1, i+swap)] = rng[min(len(array)-1, i+swap)], rng[i]
return array[rng], rng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment