728x90
import re
def solution(dartResult):
bonus = {'S' : 1, 'D' : 2, 'T' : 3}
option = {'' : 1, '*' : 2, '#' : -1}
p = re.compile('(\d+)([SDT])([*#]?)')
dart = p.findall(dartResult)
for i in range(len(dart)):
if dart[i][2] == '*' and i > 0:
dart[i-1] *= 2
dart[i] = int(dart[i][0]) ** bonus[dart[i][1]] * option[dart[i][2]]
answer = sum(dart)
return answer
def solution(dartResult):
square = {"S":1, "D":2, "T":3}
dartResult = dartResult.replace('10', 'k')
tokens = ['10' if d=='k' else d for d in dartResult]
answer = []
i = -1
for t in tokens:
if t in square:
answer[i] = anwer[i] ** square[t]
elif t == '*':
if i != 0:
answer[i-1] = answer[i-1] * 2
answer[i] = answer[i] * 2
elif t == '#':
answer[i] = answer[i] * (-1)
else: # 숫자
answer.append(int(t))
i += 1
return sum(answer)
* replace로 두 글자인 숫자를 관리
* list를 이용하여 이전 인덱스 관리
def solution(dartResult):
dart = {'S':1, 'D':2, 'T':3}
scores = []
n = 0
for i, d in enumerate(dartResult):
if d in dart:
scores.append(int(dartResult[n:i])**dart[d])
if d == "*":
scores[-2:] = [x*2 for x in scores[-2:]]
if d == "#":
scores[-1] = (-1)*scores[-1]
if not (d.isnumeric()):
n = i+1
return sum(scores)
n이 인덱스를 따라가며 두 자리 숫자 관리
import re
def solution(dartResult):
sdt = {"S":single, "D":double, "T":triple}
option = {"*":star, "#":sharp}
arr1 = re.findall("[0-9]+", dartResult)
arr2 = re.findall("[^0-9]+", dartResult)
for i in range(0, len(arr2)) :
sdt[arr2[i][0]](arr1, i)
if len(arr2[i]) > 1 :
option[arr2[i][1]](arr1, i)
return sum(arr1)
def single(num_arr, i):
num_arr[i] = int(num_arr[i])
def double(num_arr, i) :
num_arr[i] = int(num_arr[i])**2
def triple(num_arr, i) :
num_arr[i] = int(num_arr[i])**3
def star(num_arr, i):
num_arr[i] = num_arr[i]*2
if i-1 >= 0:
num_arr[i-1] = num_arr[i-1] *2
def sharp(num_arr, i):
num_arr[i] = num_arr[i]*(-1)
728x90
'알고리즘 > programmers[1]' 카테고리의 다른 글
[프로그래머스] 모의고사: python3 (0) | 2022.01.20 |
---|---|
[프로그래머스] 소수 만들기: python3 (0) | 2022.01.16 |
[프로그래머스] [1차] 다트 게임: python3 (0) | 2022.01.16 |
[프로그래머스] 하샤드 수: python3 (0) | 2022.01.16 |
[프로그래머스] 신고 결과 받기: python3 (0) | 2022.01.16 |