한 자리 정수로 이루어진 문자열 num_str이 주어질 때, 각 자리수의 합을 return하도록 solution 함수를 완성해주세요.
code - 1차
def solution(num_str):
result = 0
for i in num_str:
result += int(i)
return result
code - 2차
- sum, 리스트 컴프리헨션으로 더 간단하게 짜 봄
def solution(num_str):
return sum([int(i) for i in num_str])
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] 주사위의 개수 - 120845, list (0) | 2024.08.07 |
|---|---|
| [Python/level 1] 문자열 내 p와 y의 개수 - 12916, count() (1) | 2024.08.07 |
| [Python/level 0] n의 배수 고르기 - 120905, 리스트 컴프리헨션 (1) | 2024.08.03 |
| [Python/level 0] 암호 해독 - 120892, Slicing (0) | 2024.08.03 |
| [Python/level 0] 양꼬치 - 120830, O(n) vs O(1) 풀이법 비교 (0) | 2024.08.03 |