Waiter

  • + 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]