[HackerRank][Python3] Waiter
2018. 9. 3. 00:42 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/waiter/problem
My Solution :
#!/usr/bin/env python3
def get_primes(q):
primes = [2]
i = 3
while len(primes) < q:
is_prime = True
for prime in primes:
if i % prime == 0:
is_prime = False
break
if i < prime**2:
break
if is_prime:
primes.append(i)
i += 2
return primes
def waiter(number, q):
primes = get_primes(q)
stack_a = number
for i in range(q):
stack_before = stack_a
stack_a = []
stack_b = []
while stack_before:
num = stack_before.pop()
if num % primes[i] == 0:
stack_b.append(num)
else:
stack_a.append(num)
while stack_b:
yield stack_b.pop()
while stack_a:
yield stack_a.pop()
n, q = map(int, input().split())
number = list(map(int, input().rstrip().split()))
result = waiter(number, q)
for num in result:
print(num)
Comment :
stack 문제인지 소수 구하는 문제인지...
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Separate the Numbers (0) | 2018.09.04 |
|---|---|
| [HackerRank][Python3] Weighted Uniform Strings (0) | 2018.09.04 |
| [HackerRank][Python3] HackerRank in a String! (0) | 2018.09.03 |
| [HackerRank][Python3] Truck Tour (0) | 2018.09.03 |
| [HackerRank][Python3] Simple Text Editor (0) | 2018.09.02 |
| [HackerRank][Python3] Equal Stacks (0) | 2018.09.02 |
| [HackerRank][Python3] Maximum Element (0) | 2018.09.02 |
| [HackerRank][Python3] Matrix Layer Rotation (2) | 2018.08.08 |
최근에 달린 댓글 최근에 달린 댓글