Skip to content

Instantly share code, notes, and snippets.

@jai-dewani
Last active May 17, 2021 03:20
Show Gist options
  • Save jai-dewani/88a904c9a682adc6fb7ec1c071576194 to your computer and use it in GitHub Desktop.
Save jai-dewani/88a904c9a682adc6fb7ec1c071576194 to your computer and use it in GitHub Desktop.
Apex College Shifting Array Problem
import math
def shuffleClass(array,index):
n = len(array)
if type(array)==type([]):
if array==[]:
return []
else:
index = index % n
gcd = math.gcd(index,n)
for i in range(gcd):
temp = array[i]
cur = i
prev = i - index
while prev!=i:
prev = prev%n
array[cur] = array[prev]
cur = prev
prev = cur - index
array[cur] = temp
print(array)
else:
return []
shuffleClass([1,2,3,4,5,6],2)
shuffleClass([1,2,3,4,5,6],-2)
@jai-dewani
Copy link
Author

Thanks a lot @meekg33k for that detailed review, Yes I can see how line 3 will break the code, I overlooked that part even though I have an if statement checking if the input in array or not. I think just shifting line 3 to inside of that if statement should solve this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment