Skip to content

Instantly share code, notes, and snippets.

@Amaka202
Created May 8, 2021 14:44
Show Gist options
  • Save Amaka202/124e0c855e4ec1e2b69e2ee690ecea42 to your computer and use it in GitHub Desktop.
Save Amaka202/124e0c855e4ec1e2b69e2ee690ecea42 to your computer and use it in GitHub Desktop.
Algorithms Friday Solution for week 5
// 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]
@meekg33k
Copy link

meekg33k commented May 13, 2021

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.

@Amaka202
Copy link
Author

Thanks alot Uduak!๐Ÿ’ƒ๐Ÿป๐Ÿ’ƒ๐Ÿป

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