
오름차순으로 정렬되어있는 두 리스트 arrA, arrB를 하나의 리스트로 합치려 합니다. 단, 합친 후의 리스트도 오름차순으로 정렬되어 있어야 합니다.
예를 들어 arrA = [-2, 3, 5, 9], arrB = [0, 1, 5]인 경우 두 리스트을 오름차순으로 정렬된 하나의 리스트로 합치면 [-2, 0, 1, 3, 5, 5, 9]가 됩니다.
오름차순으로 정렬된 두 리스트 arrA와 arrB가 주어졌을 때, 두 리스트를 오름차순으로 정렬된 하나의 리스트로 합쳐서 return 하도록 solution 함수를 작성하려 합니다. 빈칸을 채워 전체 코드를 완성해주세요.
오름차순으로 정렬된 두 리스트 arrA와 arrB가 solution 함수의 매개변수로 주어집니다.
두 리스트 arrA, arrB를 오름차순으로 정렬된 하나의 리스트로 합쳐서 return 해주세요.
| arrA | arrB | return |
|---|---|---|
| [-2, 3, 5, 9] | [0, 1, 5] | [-2, 0, 1, 3, 5, 5, 9] |
def solution(arrA, arrB):
arrA_idx = 0
arrB_idx = 0
arrA_len = len(arrA)
arrB_len = len(arrB)
answer = []
while @@@:
if arrA[arrA_idx] < arrB[arrB_idx]:
answer.append(arrA[arrA_idx])
arrA_idx += 1
else:
answer.append(arrB[arrB_idx])
arrB_idx += 1
while @@@:
answer.append(arrA[arrA_idx])
arrA_idx += 1
while @@@:
answer.append(arrB[arrB_idx])
arrB_idx += 1
return answer
#The following is code to output testcase.
arrA = [-2, 3, 5, 9]
arrB = [0, 1, 5]
ret = solution(arrA, arrB);
#Press Run button to receive output.
print("Solution: return value of the function is ", ret, " .")
이번에는
빈칸 채우기문제이다.
def solution(arrA, arrB):
arrA_idx = 0
arrB_idx = 0
arrA_len = len(arrA)
arrB_len = len(arrB)
answer = []
while arrA_idx < min(arrA_len, arrB_len) and arrB_idx < min(arrA_len, arrB_len):
if arrA[arrA_idx] < arrB[arrB_idx]:
answer.append(arrA[arrA_idx])
arrA_idx += 1
else:
answer.append(arrB[arrB_idx])
arrB_idx += 1
while arrA_idx < arrA_len:
answer.append(arrA[arrA_idx])
arrA_idx += 1
while arrB_idx < arrB_len:
answer.append(arrB[arrB_idx])
arrB_idx += 1
return answer
#The following is code to output testcase.
arrA = [-2, 3, 5, 9]
arrB = [0, 1, 5]
ret = solution(arrA, arrB);
#Press Run button to receive output.
print("Solution: return value of the function is ", ret, " .")
빈칸 채우기 문제가 아니라 함수 채우기 문제 였다면, 파이썬의 sort() 함수를 사용하여 코드를 작성할 수 있다.
def solution(arrA, arrB):
arrA_idx = 0
arrB_idx = 0
arrA_len = len(arrA)
arrB_len = len(arrB)
answer = []
# 본인 작성
answer = arrA + arrB
answer.sort()
return answer
#The following is code to output testcase.
arrA = [-2, 3, 5, 9,10]
arrB = [0, 1, 5]
ret = solution(arrA, arrB);
#Press Run button to receive output.
print("Solution: return value of the function is ", ret, " .")