Waiter

Sort by

recency

|

25 Discussions

|

  • + 0 comments

    As usual, when you see the completion percentage for a question go below 90% you can suspect that it's going to be a bit nasty...

    This problem has two parts: generating the first 'q' primes, and then doing the plate sorting/stacking.

    Python 3:

    def eratosthenes():
        from collections import defaultdict
        composites = defaultdict(list)
        x = 2
        while True:
            if x in composites:
                for c in composites[x]:
                    composites[c+x].append(c)
                del composites[x]
            else:
                yield x
                composites[x*x] = [x]
            x += 1
    
    def waiter(number, q):
        plates = number
        answers = []
        primes = eratosthenes()
        for _ in range(q):
            A = []
            B = []
            prime = next(primes)
            while plates:
                plate = plates.pop()
                if plate % prime == 0:
                    B.append(plate)
                else:
                    A.append(plate)
            answers.extend(reversed(B))
            plates = A
        answers.extend(reversed(A))
        return answers
    
  • + 0 comments

    C#

    public static List<int> waiter(List<int> number, int q)
        {
            List<int> result = new List<int>();
            foreach (int p in GetFirstKPrimes(q)) {
                var b = new List<int>();
                var a = new List<int>();
                for (int i = number.Count()-1; i >= 0; i--) {
                    if (number[i] % p == 0) b.Add(number[i]);
                    else a.Add(number[i]);
                }
                b.Reverse();
                result.AddRange(b);
                
                number = a;
            }
            
            number.Reverse();
            result.AddRange(number);
            return result;
        }
        
        private static List<int> GetFirstKPrimes(int k) {
            bool[] nums = new bool[10_000];
            Array.Fill(nums, true);
            nums[0] = false;
            nums[1] = false;
            for (int i = 2; i < nums.Length; i++) {
                if (nums[i]) {
                    for (int j = 2*i; j < nums.Length; j += i)
                        nums[j] = false;
                }
            }
            
            List<int> primes = new List<int>();
            for (int i = 2; i < nums.Length; i++)
                if (nums[i]) primes.Add(i);
                
            return primes.Take(k).ToList();
        }
    
  • + 0 comments
    def waiter(number, q):
        # Write your code here
        prim = []
        
        for j in range(2,5*10**4):
            n = 2
            while j%n:
                n+=1
            if n == j:
                prim.append(j)
            if len(prim) == len(number):
                break
        ans = []
        for i in range(q):
            b = []
            for j in range(len(number)-1,-1,-1):
                if number[j]%prim[i]==0:
                    b.append(number[j])
                    number.pop(j)
            number = number[::-1]
            ans+=b[::-1]    
        return ans+number[::-1]
    
  • + 0 comments

    JavaScript...

    function *getNextPrimeNumber(){
      for(let i=2;i<Number.POSITIVE_INFINITY;i++){
        let prime=true;
        for(let j=2;j<i;j++){
          if(i%j===0){
            prime=false;
            break;
          } 
        }
        if(prime)
        yield i;
      }
    }
    function waiter(number, q) {
      let getPrime=getNextPrimeNumber();
    
      let answers=[];
    
      for(let i=0;i<q;i++){
        let prime=getPrime.next().value;
       
        let b=[];
        for(let j=0;j<number.length;j++){
          if(number[j]%prime===0){
            answers.push(number[j])
          }else{
            b.push(number[j]);
          }
        }
        
        number=b.reverse();
      }
      
      answers=[...answers,...number.reverse()]
     return answers;
    
    }
    
  • + 0 comments

    Python

    def is_prime(n):
        if n < 2:
            return False
        for i in range(2, int(n**.5) + 1):
            if n % i == 0:
                return False
        return True
    
    def generate_primes(n):
        num, count = 2, 0
        while count < n:
            if is_prime(num):
                yield num
                count += 1
            num += 1
    
    def waiter(number, q):    
        answers = []
        stack = number
        for prime in generate_primes(q):
            a = []
            b = []
            while stack:
                num = stack.pop()
                if num % prime == 0:
                    b.append(num)
                else:
                    a.append(num)
            while b:
                answers.append(b.pop())
            stack = a
        
        while stack:
            answers.append(stack.pop())
        
        return answers