• + 0 comments

    pytohn 3

    def waiter(number, q):
        # Write your code here
        answer = []
        
        point = 0
        index = 2
        
        while point < q:
            check = 0
            for now in range(1, index + 1):
                if index % now == 0:
                    check += 1
                if now < index and check == 2:
                    break
                if check == 2:
                    point += 1
                    
                    A = []
                    B = []
                    
                    for i in range(len(number)):
                        if number[i] % index == 0:
                            A.append(number[i])
                        else:
                            B.append(number[i])
                    if point < q:
                        B.reverse()
                    number = B
                    answer.extend(A)
            index += 1
         
        answer.extend(number)
        return answer