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.
defget_primes(n):primes=[2]slicing=slice(1,None)num=3whilelen(primes)<n:is_prime=Trueforprimeinprimes[slicing]:ifprime*prime>num:breakifnum%prime==0:is_prime=Falsebreakifis_prime:primes.append(num)num+=2returnprimesdefwaiter(number,q):# get 'q' number of primesprimes=get_primes(q)# initializationanswers=[]nums=number# q iterationsforiinrange(q):stack_a,stack_b=[],[]prime=primes[i]fornuminreversed(nums):ifnum%prime==0:stack_b.append(num)else:stack_a.append(num)answers.extend(reversed(stack_b))nums=stack_aanswers.extend(reversed(nums))returnanswers
So I think the hardest part was trying to find the prime numbers, I eventually found an algorithm called "The Sieve of Eratosthenes" and it worked, but would time out in some cases. Then realized we only need to determine prime numbers up to the maximum value of the numbers. So if the array of prime numbers is smaller than K, well everything just goes into Stack A always, for anything past the prime array.
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;}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
C#
So I think the hardest part was trying to find the prime numbers, I eventually found an algorithm called "The Sieve of Eratosthenes" and it worked, but would time out in some cases. Then realized we only need to determine prime numbers up to the maximum value of the numbers. So if the array of prime numbers is smaller than K, well everything just goes into Stack A always, for anything past the prime array.
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:
`