Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 공룡게임
- 게임
- dfs
- mongoose
- TypeScript
- typeORM
- cookie
- Express
- 정렬
- Python
- 자료구조
- Queue
- Sequelize
- nestjs
- game
- MySQL
- GIT
- MongoDB
- OCR
- class
- JavaScript
- Dinosaur
- Bull
- Nest.js
- react
- jest
- nodejs
- flask
- AWS
Archives
- Today
- Total
포시코딩
[프로그래머스][Lv.0] 숨어있는 숫자의 덧셈 (2) 본문
728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/120864
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
내 풀이
def solution(my_string):
if my_string.isdigit(): # (1)
return int(my_string)
answer = []
temp = ''
for str in my_string:
if str.isdigit():
temp += str
else:
answer.append(0 if len(temp) == 0 else int(temp))
temp = ''
answer.append(0 if len(temp) == 0 else int(temp)) # (2)
return sum(answer)
(1): my_string이 자연수로만 이루어졌을 경우
(2): my_string의 마지막이 자연수로 끝날 경우
다른 풀이
def solution(my_string):
s = ''.join(i if i.isdigit() else ' ' for i in my_string)
return sum(int(i) for i in s.split())
숫자가 아닌 경우에 대해 공백으로 처리하고
다시 공백을 기준으로 list로 만든 다음 sum() 처리를 해줬다.
내가 처음 구상한게 이런 방법이었는데 매우 깔끔하게 잘 푼 방법인듯
728x90
'자료구조알고리즘 > 문제풀이' 카테고리의 다른 글
[프로그래머스][Lv.0][재귀] 문자열 밀기 (0) | 2023.01.10 |
---|---|
[프로그래머스][Lv.0][재귀] 종이 자르기 (0) | 2023.01.08 |
[프로그래머스][Lv.0] 가까운 수 (0) | 2023.01.07 |
[프로그래머스][Lv.0] A로 B 만들기 (0) | 2023.01.07 |
[프로그래머스][Lv.0] 팩토리얼 (0) | 2023.01.05 |