Last active
May 21, 2021 08:38
-
-
Save toritsejuFO/496fda3b1655eac83ef954048838b27c 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
# Required function | |
def shuffle_class(pupils, pupils_to_move): | |
if pupils == None or type(pupils) != list or len(pupils) == 0 or type(pupils_to_move) != int: | |
return [] | |
# For this solution's approach, normally, we would just take the absolute value of the pupils to move, | |
# but we also take the modulus, just in case, to avoid index out of range edge case, and also avoid round trips | |
to_move = abs(pupils_to_move) % len(pupils) | |
if pupils_to_move == 0 or to_move == 0: | |
return pupils | |
back_to_front = True if pupils_to_move > 0 else False | |
length = len(pupils) | |
# We split, join (swapped) and return on the fly | |
if back_to_front: | |
return pupils[ (length - to_move) :] + pupils[ 0 : (length - to_move)] | |
else: | |
return pupils[ to_move : ] + pupils[ 0 : to_move] | |
if __name__ == '__main__': | |
pupils = [8, 5, 3, 7] | |
pupils_to_move = 2 | |
shuffled_pupils = shuffle_class(pupils, pupils_to_move) | |
print(shuffled_pupils) | |
assert shuffled_pupils == [3, 7, 8, 5] | |
pupils_to_move = -1 | |
shuffled_pupils = shuffle_class(pupils, pupils_to_move) | |
print(shuffled_pupils) | |
assert shuffled_pupils == [5, 3, 7, 8] |
Thanks for your feedback @meekg33k, really appreciate it.
Yes, there are other ways to move the elements around without creating a new array.
I wanted to try something different other than sorting in place, so I used this method because the new array created is returned immediately (ending the function call), and in the scope of the function the pupils
list passed in would be released when the function ends/exits. It was my thought that that would be optimal enough for space complexity (and this was done because I was trying to avoid tackling time complexity).
Thanks for considering my solution for the $20 prize. I've indicated interest in the raffle draw via sending a DM on twitter. Thank you again.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @toritsejuFO, thank you participating in Week 6 of #AlgorithmFridays.
Your solution, is definitely of the most correct solutions that we received. It is robust with really good edge-case handling, passes the test cases and is readable.
I see that you used slicing and concatenation for computing the final result. How expensive do you think that method is in terms of space efficiency considering that the
slice
operation creates a new array? Do you think there's another way to have moved the elements (pupils) around without needing to create a new array? Let me know what you think.So while your solution was correct, it was not the most optimal solution and sadly, there can be only 1 awardee of the $50 prize.
That said, your solution is being considered for the $20 prize via a raffle draw. The raffle draw will hold today, Friday May 21 at 3.00pm WAT (7.00 am PST)
If you are interested in being a part of the raffle draw, please send an email to [email protected] or send me a DM on Twitter @meekg33k so I can share the event invite with you.
NB: Only solutions of participants who indicated interest in the raffle draw will be considered.
Thanks once again for participating and see you later today for Week 7 of #AlgorithmFridays.