Algorithm/구현
[프로그래머스 lv 1] [1차] 다트 게임
HANNI하니
2023. 5. 30. 11:41
사용 언어 - Python3
문제 - [1차] 다트 게임
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답
단순 구현 (정답 맞춘 여부 O)
코드 길이가 너무 길어서 줄이는 연습이 필요해보인다.
def solution(dartResult):
# 해당 점수 마이너스
score = []
for i in range(1,len(dartResult)):
d = dartResult[i]
dd = dartResult[i-1]
# S D T 점수계산
if d == 'S' and dd.isdigit():
# 숫자가 10인 경우
if dd == '0' and dartResult[i-2].isdigit():
dd = 10
score.append(int(dd)**1)
elif d == "D" and dd.isdigit():
# 숫자가 10인 경우
if dd == '0' and dartResult[i-2].isdigit():
dd = 10
score.append(int(dd)**2)
elif d == "T" and dd.isdigit():
# 숫자가 10인 경우
if dd == '0' and dartResult[i-2].isdigit():
dd = 10
score.append(int(dd)**3)
# 옵션 *, #
elif d == "*":
if len(score) == 1:
score[0] *= 2
elif len(score) >= 2:
score[-1] *= 2
score[-2] *= 2
elif d == "#":
score[-1] *= -1
print(score)
return sum(score)
다른 사람의 좋은 풀이!
숫자가 10을 k로 replace 해준 후, k인 경우 10으로 바꿔준다.
point라는 새로운 리스트로 if문 조건 걸기
dartResult = dartResult.replace('10','k')
point = ['10' if i == 'k' else i for i in dartResult]
레퍼런스
- 정답 깃허브
GitHub - yyeongeun/codingtest: 코딩테스트 공부
코딩테스트 공부. Contribute to yyeongeun/codingtest development by creating an account on GitHub.
github.com