Created
May 15, 2021 10:11
-
-
Save Sijibomii/5b6d1c144d5934f5327c21a6f87ccaf4 to your computer and use it in GitHub Desktop.
ALGORITHM FIDAYS
This file contains 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 shuffleClass(pupilsList, noToBeMoved): | |
# validate inputs first | |
#check if pupils list is a valid list | |
if not isinstance(pupilsList, list): | |
return [] | |
#check if noToBeMoved is a valid integer | |
if not isinstance(noToBeMoved, int): | |
return pupilsList | |
classSize = len(pupilsList) | |
#if pupils list contains just one element or noToBeMoved is zero return it | |
if classSize <= 1 or noToBeMoved == 0: | |
return pupilsList | |
#check that the modulos of noToBeMoved and classSize is not zero if it is, return | |
if noToBeMoved >= classSize and abs(noToBeMoved) % classSize == 0: | |
return pupilsList | |
#no of steps irrespective of direction | |
noOfSteps= abs(noToBeMoved) if abs(noToBeMoved) < classSize else abs(noToBeMoved) % classSize | |
#set direction for movement | |
endToFront = True if noToBeMoved > 0 else False | |
if endToFront:#End to Front | |
c=[pupilsList.pop() for x in range(noOfSteps)] | |
d=[pupilsList.insert(0,x) for x in c] | |
return pupilsList | |
else:#FrontToEnd | |
c=[pupilsList.pop(0) for x in range(noOfSteps)] | |
d=[pupilsList.append(x) for x in c] | |
return pupilsList | |
if __name__ == "__main__": | |
print(shuffleClass([8,5,3,7], 2)) # return [3,7,8,5] | |
print(shuffleClass([4,1,3], -1)) # return [1,3,4] | |
print(shuffleClass([8,5,3,7,9,10], -106)) # return [9, 10, 8, 5, 3, 7] | |
print(shuffleClass([4,1,3,7,20,90,14], -100)) # return [3, 7, 20, 90, 14, 4, 1] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Sijibomii, thank you participating in Week 6 of #AlgorithmFridays.
Your solution, is absolutely of the most correct solutions that we received. It is robust with really decent edge-case handling, passes the test cases and is readable.
I see that you used a series of
pop
andinsert
operations for computing the final result. How expensive do you think those are in terms of time efficiency? Do you think there's a way we could have reduced the number of times,noOfSteps
, we had to iterativelypop
andinsert
? 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.