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)
@meekg33k
Copy link

Hello @jai-dewani, this seems like your first time of participating in Week 6 of #AlgorithmFridays. Thanks for being a part of it.

This is a really decent attempt at a solution for Apex College, and your solution passes most of the test cases. However, there is only one test case that it didn't pass:

  • When array has a None value, your solution breaks on line 3. This is because in coding up your solution, you made an assumption that array cannot have a None value. Ideally, you want to write robust code that includes validation for invalid/unexpected input types without throwing any runtime errors.

I understand that this solution was made with a lot of assumptions of how certain things should be implemented but I think it's always best to check in with your interviewer before making assumptions on how edge cases should be implemented. I wrote about that in this article, you might find it helpful.

Apart from that, this was good! Please let me know your thoughts.

@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