π‘ Python 3
[1μ°¨] λ€νΈ κ²μ [Link]
def solution(dR):
dR = dR.replace('10', 'N')
result = []
for i, s in enumerate(dR):
if s == 'N': s = '10'
if s.isdigit():
k = int(s)
if dR[i+1] == 'D': k **= 2
elif dR[i+1] == 'T': k **= 3
result.append(k)
if i ==2 and s == '*':
r1 = result.pop()*2
result.append(r1)
elif i != 2 and s == '*':
r1 = result.pop()*2
r2 = result.pop()*2
result.append(r2)
result.append(r1)
elif s == '#':
result.append(result.pop()*(-1))
return sum(result)
μ κ·ννμ νμ©.
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
\d+
: νλ μ΄μ μ°κ²°λ μ«μ
\d
: μ«μ 1κΈμ+
: νλ μ΄μfindall()
: μ κ·μκ³Ό 맀μΉλλ λͺ¨λ λ¬Έμμ΄μ 리μ€νΈνμμΌλ‘ 리ν΄
κΈλ§λ΄μλ λ¬΄μ¨ μλ¦°μ§ μ λͺ¨λ₯΄κ² μ΄μ print
μ°μ΄λ΄€λ€.
dR = "1S2D*3T"
def solution(dartResult):
import re
bonus = {'S' : 1, 'D' : 2, 'T' : 3}
option = {'' : 1, '*' : 2, '#' : -1}
p = re.compile('(\d+)([SDT])([*#]?)')
dart = p.findall(dartResult)
print(dart)
# output
[('1', 'S', ''), ('2', 'D', '*'), ('3', 'T', '')]
[νμ΄μ¬] μ κ·ννμ(regular expression)
μ κ·ννμ #2
04 λͺ¨λ λ¬Έμμ΄ ν¨ν΄ μ°ΎκΈ° (findall)