Created
September 3, 2016 16:02
-
-
Save cjmcmurtrie/b68b66fd03758c3b3de2a9856dbc73b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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