I want to permute the elements of a list according to their index modulo 3, so for example the list:
[0,1,2,3,4,5,6,7,8]
should be reordered into:
[0,3,6,1,4,7,2,5,8]
and in general:
[A0, A1, A2, A3, A4, A5, A6, A7, A8]
should become:
[A0, A3, A6, A1, A4, A7, A2, A5, A8]
I have tried to use the following code:
def arr_sort(arr, algo):
arrtmp = arr
arrlen = len(arr)
if algo == 1:
return arr
if algo == 2:
count = 0
while count < (arrlen - 1):
for index, val in enumerate(arr):
if index % 3 == 0:
arrtmp[count] = val
count += 1
for index, val in enumerate(arr):
if index % 3 == 1:
arrtmp[count] = val
count += 1
for index, val in enumerate(arr):
if index % 3 == 2:
arrtmp[count] = val
count += 1
return arrtmp
It's not working properly, as arr
gets changed throughout the while loop, and I can't really see why.
(also I know that I could do the "f index % ...
bits in a for loop as well, but it should work anyways, right?)
Is there a pre-existing function that could do that instead?