## Friday, April 8, 2016

### Waiter - HackerRank

https://www.hackerrank.com/challenges/waiter
You are a waiter at a party. There are $N$ stacked plates. Each plate has a number written on it. You start picking up the plates from the top one by one and check whether the number written on the plate is divisible by $P$(a prime). The starting value of $P$ is $2$. If the number is divisible, you stack that plate separately with other $P$ divisible plates. If not, you stack that plate separately with the other plates that are not divisible by $P$.
In the next iteration, the value of $P$ changes to the next prime number after $P$. You check the plates from the pile that were not divisible by $P$ in the last iteration. You repeat this process a number of times. By doing this process $Q$ times, you end up getting different piles of plates. The plates that are not divisible by the ${Q}^{th}$ prime (which is our last iteration), from the last pile of plates. Say you have $M$ (it is clear that $M$ is either $Q$ or $Q+1$) different piles of plates. Starting from the first pile, print the number written on the plate while removing plates from a pile in the same order as described above. Do this process for all the $M$ piles. Print one value in a single line.
http://www.martinkysel.com/hackerrank-waiter-solution/
First of all, generate primes using Sieve, or copy-paste a list of pre-generated primes. After each iteration the order of the remaining plates is reversed (you put the last on top and once finished you start from the top). That means that the solution has to have a stack, not a queue.
generated_primes = [...]

def solveWaiter(N, Q, numbers):
    stack = []

    for value in numbers:
        if value % generated_primes[0] != 0:
            stack.append(value)
        else:
            print value


    for prime_idx in xrange(1, Q):
        leftover = []

        while stack:
            value = stack.pop()
            if value % generated_primes[prime_idx] != 0:
                leftover.append(value)
            else:
                print value
        stack = leftover

    for value in stack:
        print value

def main():
    N, Q = map(int, raw_input().split())

    numbers = map(int, raw_input().split())

    solveWaiter(N, Q, numbers)

    public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int N=sc.nextInt(), Q=sc.nextInt();
Stack<Integer> pile=new Stack<Integer>();
for(int i=0; i<N; i++)
pile.push(sc.nextInt());
List<Stack<Integer>> piles=new ArrayList<Stack<Integer>>();
process(piles, Q);
output(piles);
}
public static void process(List<Stack<Integer>> piles, int Q) {
List<Integer> primes=getQPrimes( Q );
for(int p: primes){
Stack<Integer> stack=piles.remove(piles.size()-1);
Stack<Integer> divisable=new Stack<Integer>();
Stack<Integer> notdivzab=new Stack<Integer>();
while(!stack.empty()) {
int t=stack.pop();
if(t%p==0)
divisable.push(t);
else
notdivzab.push(t);
}
}
}
public static void output(List<Stack<Integer>> piles) {
StringBuilder sb=new StringBuilder();
for(Stack<Integer> stack: piles) {
while(!stack.empty())
sb.append(stack.pop()+"\n");
}
System.out.println(sb);
}
public static List<Integer> getQPrimes(int q) {
List<Integer> res=new ArrayList<Integer>();
BigInteger bi = BigInteger.valueOf(1);
for(int i=0; i<q; i++){
bi=bi.nextProbablePrime();//\\
}
return res;
}

    static int nextPrimeNumber(int num,int idx,int[] primes){
if(num == 2) return 3;
while(true){
num = num + 2;
boolean isPrime = true;
for(int i = 0; i <= idx; ++i){
if(num%primes[i] == 0){
isPrime = false;
break;
}
}
if(isPrime) break;
}
return num;
}