We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I guess that this task is designed to help us prepare for the real challenges of engineering work, as it is a great exercise in dealing with confusing customer requirements ;)
What helped me understand the baffling description and solve the problem was following the proposed analogy with plates, and visualizing how they are being transferred from/to different stacks by the waiter (I even drew a couple of diagrams on a sheet of paper).
JavaScript solution:
functionwaiter(number,q){// Write your code hereconstanswers=[];constprimes=[];letnum=2;while(primes.length<q){letisPrime=true;for(letdiv=2;num>div;div++){if(num%div===0){isPrime=false;break;}}if(isPrime)primes.push(num);num++;}functionmovePlatesOneByOne(fromStack,toStack){for(leti=fromStack.length-1;i>=0;i--){toStack.push(fromStack[i]);}fromStack.length=0;}constplates=number.reverse();conststackA=[];// divisible by prime: falseconststackB=[];// divisible by prime: truefor(letprimeofprimes){if(stackA.length){movePlatesOneByOne(stackA,plates);}for(letplateofplates){if(plate%prime===0){stackB.push(plate);}else{stackA.push(plate);}}plates.length=0;if(stackB.length){movePlatesOneByOne(stackB,answers);}}if(stackA.length){movePlatesOneByOne(stackA,answers);}returnanswers;}
Problem description is a bit of a hustle to understand and worded unnecessarily complicated.
A simple solution with Modern C++ and horrendous code otherwise.
vector<int>primes{2,3,5,7,11,13};boolisPrime(intn){for(intp:primes){if(p>n/2){returntrue;}if(n%p==0){returnfalse;}}returntrue;}intgetNthPrime(intn){if(n<=primes.size()){returnprimes[n-1];}// If there are more then one prime is missing calculate those// In this question this should not be neededif(n+1>primes.size()){getNthPrime(n-1);}intcandidate=primes.back()+2;while(!isPrime(candidate)){candidate+=2;}primes.push_back(candidate);returncandidate;}vector<int>waiter(vector<int>numbers,intq){vector<int>answers;for(inti=1;i<q+1;i++){intprime=getNthPrime(i);autoit=stable_partition(numbers.begin(),numbers.end(),[prime](intn){returnn%prime;});if(i%2==1){answers.insert(answers.end(),it,numbers.end());}else{autorIt=make_reverse_iterator(it);answers.insert(answers.end(),numbers.rbegin(),rIt);}numbers.erase(it,numbers.end());}if(q%2==1){answers.insert(answers.end(),numbers.begin(),numbers.end());}else{answers.insert(answers.end(),numbers.rbegin(),numbers.rend());}returnanswers;}
Java solution, using LinkedList and Iterator to make remove operation O(1) while avoid copy to stack as well.
I guess that this task is designed to help us prepare for the real challenges of engineering work, as it is a great exercise in dealing with confusing customer requirements ;)
What helped me understand the baffling description and solve the problem was following the proposed analogy with plates, and visualizing how they are being transferred from/to different stacks by the waiter (I even drew a couple of diagrams on a sheet of paper).
JavaScript solution:
`
Problem description is a bit of a hustle to understand and worded unnecessarily complicated.
A simple solution with Modern C++ and horrendous code otherwise.
Nominated as the single worst problem description, across all coding practice websites that ever have or ever will exist.
Python solutuion