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 |
Tags
- 코딩테스트
- 프로그래머스
- 가상메모리 관리
- dfs
- python
- 파이썬
- 다이나믹 프로그래밍
- 운영체제
- programmers
- dict
- level0
- 딕셔너리
- level3
- DP
- level2
- 브루트포스
- 가상메모리
- 백준
- BOJ
- MYSQL
- level1
- 구현
- 재귀
- 힙
- 다익스트라
- N과M
- 그리디
- 스택
- BFS
- 수학
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level0 [다항식 더하기] 파이썬 본문
https://school.programmers.co.kr/learn/courses/30/lessons/120863
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#프로그래머스 다항식 더하기
def solution(polynomial):
x_cnt=0
digit=0
arr=list(map(str,polynomial.split()))
for val in arr:
if val!='+':
if 'x' in val:
if len(val)==1:
x_cnt+=1
else:
x_cnt+=int(val[:-1])
else:
digit+=int(val)
if x_cnt==0:
answer="{}".format(digit)
elif x_cnt==1:
if digit!=0:
answer="x + {}".format(digit)
else:
answer="x"
elif x_cnt>=1:
if digit!=0:
answer="{}x + {}".format(x_cnt,digit)
else:
answer="{}x".format(x_cnt)
return answer
여러 케이스를 개별로 생각해서 해결한다.
'코테 > 프로그래머스 level0' 카테고리의 다른 글
프로그래머스 level0 [저주의 숫자 3] 파이썬 (0) | 2023.03.06 |
---|---|
프로그래머스 level0 [특이한 정렬] 파이썬 (0) | 2023.03.05 |
프로그래머스 level0 [최빈값 구하기] 파이썬 (0) | 2023.03.05 |
프로그래머스 level0 [OX퀴즈] 파이썬 (0) | 2023.03.05 |
프로그래머스 level0 [분수의 덧셈] 파이썬 (0) | 2023.03.05 |