Skip to content

Instantly share code, notes, and snippets.

@Abiola-Farounbi
Last active May 21, 2021 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abiola-Farounbi/177349387dddc206d878da5540a03d24 to your computer and use it in GitHub Desktop.
Save Abiola-Farounbi/177349387dddc206d878da5540a03d24 to your computer and use it in GitHub Desktop.
This function returns the shuffled array
// Apex college needs your help in writing a logic to shuffle pupils on an assembly line
// by moving a number of pupils either to the front of the line or to the end of the line.
const shufflePupils = (pupils,val) => {
// Test cases for null and empty array
if (!pupils || pupils.length == 0) return []
// test case for 0 value
if(val == 0) return pupils
// define array to be shuffled
let shuffleArr
// for positive numbers less than length of the array
if(val > 0 && val < pupils.length){
shuffleArr = pupils.splice(-val)
return [...shuffleArr,...pupils]
}
// for positive numbers greater than length of the array
else if(val > 0 && val > pupils.length){
shuffleArr = pupils.splice(-(val % pupils.length))
return [...shuffleArr,...pupils]
}
// for negative numbers less than length of the array
else if(Math.sign(val) === -1 && Math.abs(val) < pupils.length){
shuffleArr = pupils.splice(0,Math.abs(val))
return [...pupils,...shuffleArr]
}
// for negative numbers greater than length of the array
else if(Math.sign(val) === -1 && Math.abs(val) > pupils.length){
shuffleArr = pupils.splice(0, Math.abs(val % pupils.length))
return [...pupils,...shuffleArr]
}
}
console.log(shufflePupils([4, 1, 3], -1)); //[1,3,4]
console.log(shufflePupils([8,5,3,7],2)) //[3,7,8,5]
@meekg33k
Copy link

Hello @Abiola-Farounbi, thank you participating in Week 6 of #AlgorithmFridays.

Your solution, is one of the correct solutions that we received. It is robust with good edge-case handling, passes the test cases.

I see that you used spliced using the value of val and then used the spread operator for computing the final result. How expensive do you think the splice operation is? Do you think there's a way we could have reduced the number of elements - val (pupils) we needed to move around?

One other thing to note is that it always helps to try to use good variable naming. For example noOfPupilsToBeMoved reads better than val. Do 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 uduak@meekg33k.dev 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.

@Abiola-Farounbi
Copy link
Author

Hi ,
Thanks for the feedback, I would work better on it

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