일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 이분탐색
- programmers
- 위클리 챌린지
- divmod
- 정규식
- lambda
- DateTime
- 그리디
- 파이썬
- dfs
- 백준
- 동적 계획법
- Set
- 수학
- 다익스트라
- BFS
- java
- 카카오
- Combinations
- heapq
- KAKAO BLIND RECRUITMENT
- 재귀함수
- Zip
- 정렬
- python
- 자바
- backjoon
- 프로그래머스
- 추석맞이 코딩챌린지
- Re
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