아무 원소도 들어있지 않은 빈 배열 X가 있습니다. 양의 정수 배열 arr가 매개변수로 주어질 때, arr의 앞에서부터 차례대로 원소를 보면서 원소가 a라면 X의 맨 뒤에 a를 a번 추가하는 일을 반복한 뒤의 배열 X를 return 하는 solution 함수를 작성해 주세요.
code - 1차
def solution(arr):
result = []
for i in arr:
for _ in range(i):
result.append(i)
return result
code - 2차
def solution(arr):
x = []
for i in arr:
x += [i] * i
return x
- 이중 for문을 하지 않는 방식으로 코드 리팩토링하였습니다.
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/level 0] 빈 배열에 추가, 삭제하기 - 181860, enumerate, extend, del (1) | 2024.08.31 |
---|---|
[Python/level 1] 두 정수 사이의 합 - 12912, sum + 리스트 컴프리헨션 vs. for 반복문 (0) | 2024.08.31 |
[Python/level 0] 숫자 찾기 - 120904, enumerate (0) | 2024.08.31 |
[Python/level 1] 정수 내림차순으로 배치하기 - 12933, sorted(reverse = True) (0) | 2024.08.31 |
[Python/level 0] 문자열 잘라서 정렬하기 - 181866, filter(function, iterable) (1) | 2024.08.31 |