Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- react
- C++
- 알고리즘
- 항해플러스
- react-router
- json-server
- maeil-mail
- Python
- 코딩테스트합격자되기
- useDispatch
- axios
- react-redux
- 프로그래머스
- Get
- redux-saga
- 매일메일
- redux-toolkit
- Algorithm
- 자바
- createSlice
- redux
- 테코테코
- JavaScript
- 리액트
- java
- SW
- 항해99
- programmers
- sw expert academy
- 이코테
Archives
- Today
- Total
Binary Journey
[프로그래머스] 다트게임 본문
반응형
출처: https://programmers.co.kr/learn/challenges
** Javascript
function solution(dartResult) {
const bo = dartResult.split(/\d/g).filter((item) => item.length);
const s = dartResult.split(/[SDT]|\*|\#/g).filter((item) => item.length);
let strg = [];
for (let i = 0; i < 3; i++) {
const [ bonus, option ] = bo[i].split("");
let score = Number(s[i]) ** "_SDT".indexOf(bonus);
strg.push(score);
if (option === "*") {
for (let j = i - 1; j < i + 1; j++) {
strg[j] = strg[j] * 2;
}
}
if (option === "#") {
strg[i] = strg[i] * -1;
}
}
return strg.reduce((acc, curr) => acc += curr, 0);
}
** Python
import re
def solution(dartResult):
s = re.findall(r'\d+', dartResult)
bo = [i for i in re.split(r'(\d+)', dartResult) if not i.isdigit()][1:]
_sum = [0, 0, 0]
for i in range(len(s)):
_sum[i] = int(s[i])
for j in bo[i]:
if '_SDT'.find(j) > 0:
_sum[i] = _sum[i] ** '_SDT'.find(j)
elif j == "*":
_sum[i] = _sum[i] * 2
if i != 0:
_sum[i-1] = _sum[i-1] * 2
else:
_sum[i] = _sum[i] * -1
return sum(_sum)
반응형
'프로그래머스 > level 1' 카테고리의 다른 글
[프로그래머스] 나누어 떨어지는 숫자 배열 (0) | 2021.09.23 |
---|---|
[프로그래머스] 두 정수 사이의 합 (0) | 2021.09.23 |
[프로그래머스][위클리챌린지] 6주차 복서 정렬하기 (0) | 2021.09.12 |
[프로그래머스] 문자열 내 p와 y의 개수 (0) | 2021.09.07 |
[프로그래머스] 내적 (0) | 2021.09.01 |