Last active
May 16, 2021 18:43
-
-
Save chygoz2/144716c43d42e727b060c80ec2d2ebf6 to your computer and use it in GitHub Desktop.
Algorithm week 6 challenge
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
const shuffleClass = (classList, count) => { | |
if (isNaN(count) || !Array.isArray(classList)) throw new Error('Invalid input '); | |
let newClassList = []; | |
if (count === 0 || Math.abs(count) >= classList.length) return classList; | |
if (count > 0) { | |
for (let i = classList.length - count; i < classList.length; i++) { | |
newClassList.push(classList[i]); | |
} | |
for (let i = 0; i < classList.length - count; i++) { | |
newClassList.push(classList[i]); | |
} | |
} else { | |
count = Math.abs(count); | |
for (let i = count; i < classList.length; i++) { | |
newClassList.push(classList[i]); | |
} | |
for (let i = 0; i < count; i++) { | |
newClassList.push(classList[i]); | |
} | |
} | |
return newClassList; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @chygoz2, thank you for participating in Week 6 of #AlgorithmFridays.
This is a really good attempt at coming up with a solution for Apex College, and your solution passes most of the test cases. I particularly like your robust checks on lines 2 and 6.
However, your solution doesn't pass the following test cases:
When
classList
has anull
value. In your solution, you threw an error but the preferred approach would have been to log an error and return an empty array instead.The other test your solution doesn't pass is for cases where
count
has a value greater than the size of theclassList
. For such cases, your solution returns theclassList
. For example,shuffleClass([2, 3], 3); // yours would return [2, 3] instead of [3, 2]
. The expectation is that when the value ofcount
is greater than theclassList
, you should think of it as shuffling the entire pupils list for as many times as is possible untilcount
becomes less than the size ofclassList
. In mathematical terms, that would becount = count % classList.length
.It's always best to check in with your interviewer before making assumptions on how edge cases should be handled. I wrote about that in this article, you might find it helpful.
Apart from that, this was a good attempt. Please let me know your thoughts.