Skip to content

Commit 9bc8d35

Browse files
authored
Create 4.py
1 parent bc28acd commit 9bc8d35

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

20/4.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 사전에 정렬된 리스트 A와 B 선언
2+
n, m = 3, 4
3+
a = [1, 3, 5]
4+
b = [2, 4, 6, 8]
5+
6+
# 리스트 A와 B의 모든 원소를 담을 수 있는 크기의 결과 리스트 초기화
7+
result = [0] * (n + m)
8+
i = 0
9+
j = 0
10+
k = 0
11+
12+
# 모든 원소가 결과 리스트에 담길 때까지 반복
13+
while i < n or j < m:
14+
# 리스트 B의 모든 원소가 처리되었거나, 리스트 A의 원소가 더 작을 때
15+
if j >= m or (i < n and a[i] <= b[j]):
16+
# 리스트 A의 원소를 결과 리스트로 옮기기
17+
result[k] = a[i]
18+
i += 1
19+
# 리스트 A의 모든 원소가 처리되었거나, 리스트 B의 원소가 더 작을 때
20+
else:
21+
# 리스트 B의 원소를 결과 리스트로 옮기기
22+
result[k] = b[j]
23+
j += 1
24+
k += 1
25+
26+
# 결과 리스트 출력
27+
for i in result:
28+
print(i, end=' ')

0 commit comments

Comments
 (0)