Sort by

recency

|

368 Discussions

|

  • + 0 comments
    vector<int> waiter(vector<int> number, int q) {
        
        vector<int> v{};
        stack<int> snumber;
        vector<int> output;
        
        for(auto a : number)
        {
            cout<<a<<" ";
            snumber.push(a);
        }
        auto getprimes =[](int q){
                deque<int> primes{};
                int num = 2;
                while (static_cast<int>(primes.size()) < q) {
                    bool is_prime = true;
                    for (int i = 2; i <= std::sqrt(num); ++i) {
                        if (num % i == 0) {
                            is_prime = false;
                            break;
                        }
                    }
                    if (is_prime) {
                        primes.push_back(num);
                    }
                    num++;
                }
                return primes;
        };
        deque<int> nprimes = getprimes(q);
        while (!nprimes.empty()) {
            int prime = nprimes.front();
            nprimes.pop_front();
                stack<int> a;
                stack<int> b;
                while(!snumber.empty())
                {
                   int top = snumber.top();
                   snumber.pop();
                   if(top%prime==0)
                        b.push(top);
                   else
                        a.push(top);
                }
                while(!b.empty())
                {
                    int top = b.top();
                    b.pop();
                    output.push_back(top);
                }
                snumber=a;
        }
        
        while(!snumber.empty())
        {
            int top = snumber.top();
            snumber.pop();
            output.push_back(top);
        
        }
        return output;
    }
    
  • + 0 comments
    def generate_primes(q):
        primes = []
        num = 2
        while len(primes) < q:
            is_prime = True
            for p in primes:
                if num % p == 0:
                    is_prime = False
                    break
            if is_prime:
                primes.append(num)
            num += 1
        return primes
    
    def waiter(arr_p, q):
        primes = generate_primes(q)  
        answers = []
    
        for i in range(q):
            A, B = [], []
            prime = primes[i]
    
            while arr_p:
                plate = arr_p.pop()  
                if plate % prime == 0:
                    B.append(plate)  
                else:
                    A.append(plate)  
    
            answers.extend(reversed(B))
            arr_p = A 
        answers.extend(reversed(arr_p))
    
        return answers
    
    if __name__ == '__main__':
        n, q = map(int, input().split())  
        arr_p = list(map(int, input().split()))  
    
        result = waiter(arr_p, q)  
        for i in result:
            print(i)
    
  • + 0 comments

    Python 3. Sieve of Eratosthenes

    n, q = list(map(int, input().split()))
    
    number = list(map(int, input().rstrip().split()))
    length = len(number)
    
    def get_primes(n, q):
        # Primes using Sieve of Eratosthenes
        primes = list()
        sieve = [True] * (n+1)
        for p in range(2, n+1):
            if sieve[p]:
                primes.append(p)
                for i in range(p, n+1 ,p):
                    sieve[i] = False
            if len(primes) >= q: # we only need the first q prime numbers
                break
        return primes
        
    primes = get_primes(max(number), q)
    A = []
    B= []
    for _ in range(q):
        p = primes.pop(0) # the first prime number in the list
        for elem in number:
            if elem % p == 0: # check divisibility with prime number
                B.append(elem)
            else:
                A.append(elem)
                
        number = list(reversed(A)) # reverse the remainder elements for next iteration
        A = [] 
    
    if len(B) == length:
        print(*B, sep="\n")
    else:
        print(*(B + list(reversed(number))), sep="\n")    
    
  • + 0 comments

    Python 3 solution with Sieve of Eratosthenes:

    def get_prime(n):
        if n < 2:
            return [];
        nums = [True]*(n+1)
        nums[0], nums[1] = False, False
        p = 2
        while(p*p<=n):
            if nums[p]:
                for i in range(p*p, n+1, p):
                    nums[i] = False
            p+=1
        primes = [num for num, prime in enumerate(nums) if prime]
        return primes
            
    primes = get_prime(10001)
    
    def waiter(number, q):
        # Write your code here
        answers = []
        A = number
        
        for i in range(q):
            if not A:
                break
            prime = primes[i]
            B=[]
            new_A=[]
            for plate in reversed(A):
                if plate%prime==0:
                    B.append(plate)
                else:
                    new_A.append(plate)
            answers.extend(reversed(B))
            A=new_A
        answers.extend(reversed(A))
        return answers
    
  • + 0 comments

    def generate_sieve(): constraint = pow(10, 4) boolean_sieve = [True] * (constraint + 1) boolean_sieve[0] = boolean_sieve[1] = False sieve = []

    for i in range(len(boolean_sieve))[2:]:
        if boolean_sieve[i]:            
            for j in range(len(boolean_sieve))[i*2::i]:
                boolean_sieve[j] = False            
    
        if i * i >= constraint:
            break
    
    for i in range(len(boolean_sieve)):
        if boolean_sieve[i]:
            sieve.append(i)
    
    return sieve
    

    def waiter(number, q): answers = [] sieve = generate_sieve() A = number

    for i in range(q):
        next_A = []
        B = []
    
        while A:
            plate = A.pop()          
            if plate % sieve[i] == 0:
                B.insert(0, plate)       
            else:
                next_A.append(plate)
    
        answers.extend(B)
        A = next_A
    
    answers.extend(reversed(A))
    
    return answers