일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- java
- backjoon
- 위클리 챌린지
- python
- DateTime
- BFS
- KAKAO BLIND RECRUITMENT
- 추석맞이 코딩챌린지
- 동적 계획법
- divmod
- Combinations
- 정규식
- 그리디
- 백준
- 파이썬
- Re
- programmers
- dfs
- 수학
- 다익스트라
- Set
- 정렬
- 프로그래머스
- 이분탐색
- heapq
- 재귀함수
- 자바
- lambda
- Zip
- 카카오
Archives
- Today
- Total
상상쓰
[프로그래머스] [1차] 다트 게임 본문
https://programmers.co.kr/learn/courses/30/lessons/17682
코딩테스트 연습 - [1차] 다트 게임
programmers.co.kr
주어진 다트 점수 중 'S' 는 '**1', 'D' 는 '**2', 'T' 는 '**3' 과 같이 '*' 와 '#' 도 마찬가지로 문제에서 주어진 환산 방식을 참고하여 수식으로 된 문자열로 바꿔준다.
reduce 와 eval 함수를 통해 구한 점수의 합을 구한다.
from functools import reduce
def solution(dartResult):
answer = 0
score = ''
bonus = {'S' : '**1' , 'D' : '**2', 'T' : '**3'}
result = []
for i in dartResult:
if i not in ['S', 'D', 'T', '*', '#']:
score += i
else:
if i in ['S', 'D', 'T']:
score += bonus[i]
result.append(score)
score = ''
else:
if i == '*':
result[-1] += '*2'
if len(result) > 1: result[-2] += '*2'
else:
result[-1] += '*(-1)'
answer = int(reduce(lambda x, y : str(eval(x) + eval(y)), result))
return answer
print(solution('1S2D*3T')) # 37
'Coding Test' 카테고리의 다른 글
[프로그래머스] [3차] 압축 (0) | 2021.09.03 |
---|---|
[프로그래머스] [3차] 방금그곡 (0) | 2021.09.01 |
[프로그래머스] [1차] 비밀지도 (0) | 2021.08.30 |
[프로그래머스] 5주차_모음사전 (0) | 2021.08.30 |
[프로그래머스] [1차] 캐시 (0) | 2021.08.28 |
Comments