Created
May 8, 2021 14:44
-
-
Save Amaka202/124e0c855e4ec1e2b69e2ee690ecea42 to your computer and use it in GitHub Desktop.
Algorithms Friday Solution for week 5
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
// A school has two classes A and B. Each class has the ages of its students in | |
// a sorted list. | |
// The school decides to join both classes and needs you to help them write a function | |
// that merges both lists into one such that the students' ages still remain | |
// sorted in an increasing order | |
// PS: Do not use in buit sort function | |
const joinBothClasses = (classA, classB) => { | |
if(!classA) return []; | |
if(!classB) return classA; | |
if(!Array.isArray(classA)) return 0; | |
if(!Array.isArray(classB)) return 0; | |
let newClassList = []; | |
let classAIndex = 0 | |
let classBIndex = 0 | |
let currentIndex = 0 | |
while(currentIndex < (classA.length + classB.length)){ | |
let isClassAremaning = classAIndex >= classA.length; | |
let isClassBremaning = classBIndex >= classB.length; | |
if(!isClassAremaning && (isClassBremaning || (classA[classAIndex] < classB[classBIndex]))){ | |
newClassList[currentIndex] = classA[classAIndex] | |
classAIndex++ | |
}else{ | |
newClassList[currentIndex] = classB[classBIndex] | |
classBIndex++ | |
} | |
currentIndex++ | |
} | |
return newClassList; | |
} | |
joinBothClasses([13, 15, 19], [11, 13, 19]) // [11,13,13,15,18,19] |
Thanks alot Uduak!๐๐ป๐๐ป
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Amaka202, thank you for participating in Week 5 of #AlgorithmFridays.
Congratulations! ๐๐, your solution has been selected as one of the 12 winning solutions for Week 5 of #AlgorithmFridays.
Your solution was selected because it is clean, robust, had the best time-efficiency and passes all of the test cases. I particularly like your variable naming and how readable your code is. Really clean!
However since only 3 solutions can be awarded the $20 prize, there will be a 'raffle draw' tomorrow at 9.00 am WAT (1.00 am PST) to select 3 out of the 12 solutions in a fair manner.
If you will to participate in the raffle draw, please send an email to uduak@meekg33k.dev or send me a DM on Twitter @meekg33k so I can share the meeting link with you.
Congratulations once again and thank you for participating. See you tomorrow for Week 6 of #AlgorithmFridays.