Waiter

Sort by

recency

|

59 Discussions

|

  • + 0 comments
    def get_primes(n):
        a = [True] * n
        for i in range(3, int(n**0.5)+1, 2):
            if a[i]:
                a[i**2::i*2] = [False] * int(((n-i**2-1)/(i*2)+1))
        return [2] + [i for i in range(3, n, 2) if a[i]]
    
    def waiter(number, q):
        result, primes = [], get_primes(9734)
        for i in range(q + 1):
            b = number if i == q else [number.pop(j) for j in range(len(number) - 1, -1, -1) if number[j] % primes[i] == 0]
            result += b if i % 2 else b[::-1]
        return result
    
  • + 0 comments

    Java 8 The hardest part of the problem is generating the prime numbers. I used Sieve of Atkin algorithm to generate the list of prime numbers and used the max value in the number list. He is my solution.

    public static List<Integer> waiter(List<Integer> number, int q) {
            List<Integer> results = new ArrayList<>();
            
            // Find max value in the numbers list
            int max = number.stream().max( Integer::compareTo ).orElse(2) ;
            List<Integer> primes = sieveOfAtkin( max ) ;
              
            Stack<Integer> ai = new Stack<>();
            Stack<Integer> bi = new Stack<>();
            
            for(int k=0; k<q; ++k ) {
                if (k<primes.size()) {
                    Integer prime = primes.get(k);
                    System.out.println("prime: "+prime) ;
              
                    while(!number.isEmpty()) {
                        Integer value = number.remove(number.size()-1);
                        if (value%prime == 0)
                            bi.push(value);
                        else
                            ai.push( value );
                    }
                    stackToList(bi,results );
                    number.addAll(ai) ;
                    ai.clear();
                } else {
                    
                    // All of the rest of the primes numbers are larger 
                    // then any value in the number list. Just need to calculate 
                    // the number of value reversals.
                    if ( (q-k)/2 == 0 )
                        Collections.reverse(number);
                    break;
                }
            }
    
            Collections.reverse(number);
            results.addAll(number);
            
            return results;
        }
        
        private static void stackToList(Stack<Integer> stack, List<Integer> list) {
            while(!stack.isEmpty()) {
                list.add(stack.pop()) ;
            }
        }
    
        /**
         * Creates a list of Prime numbers.
         * Based off of Sieve Of Atkin algorithm
         * 
         * @param limit - top possible prime number
         * @return List of prime numbers from 2 too limit inclusive.
         */
        private static List<Integer> sieveOfAtkin( final int limit ) {
            List<Integer> primes = new ArrayList<>();
            boolean[] sieve = new boolean[limit+1] ;
            
            // Special case calculations
            if ( sieve.length > 2 )
                sieve[2] = true;
            if ( sieve.length > 3 )
                sieve[3] = true;
    
            // Caching each case 
            for ( int x=1; x*x <= limit; ++x ) {
                for ( int y=1; y*y <=limit; ++y ) {
                    int xx = x*x;
                    int yy = y*y;
                        
                    int n = (4*xx)+yy;
                    if (n<=limit && ( n % 12==1 || n % 12==5 ))
                        sieve[n] ^= true;
                    
                    n = (3*xx)+yy;
                    if (n<=limit && ( n % 12==7 ))
                        sieve[n] ^= true;
                    
                    n = (3*xx)-yy ;
                    if ( x > y && n <= limit && n % 12 == 11 )
                        sieve[n] ^= true;
                }
            }
            
            // Marking multiples out 
            for ( int r = 5; r*r <=limit; ++r ) {
                if (sieve[r]) {
                    for ( int i = r*r; i<= limit; i += r*r ) {
                        sieve[i] = false;
                    }
                }
            }
            
            for ( int i = 2; i <= limit; ++i ) {
                if (sieve[i])
                    primes.add(i) ;
            }
            
            return primes;
        }
    
  • + 0 comments

    Here is hackerrank waiter problem solution in Python, java, C++, C and javascript

  • + 0 comments

    can anyone help with the understanding of the problem?

  • + 0 comments

    I think the largest challenge could be the prime numbers generating.