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
- 이코테
- java
- Get
- redux
- json-server
- redux-toolkit
- SW
- 항해플러스
- C++
- 테코테코
- 자바
- react-router
- 프로그래머스
- 항해99
- axios
- useDispatch
- 리액트
- 코딩테스트합격자되기
- sw expert academy
- programmers
- 매일메일
- createSlice
- redux-saga
- 알고리즘
- react-redux
- JavaScript
- react
- maeil-mail
- Algorithm
- Python
Archives
- Today
- Total
Binary Journey
[프로그래머스][위클리챌린지] 5주차 모음 사전 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
규칙 찾기 어려울 땐 규칙 보일 때까지 나열해보는 게 답
하지만 알기 쉽게 나열해야 한다.
AEIOU 로 보기 어려워서 나는 12345 로 바꾼뒤에 규칙을 찾아보았다.
( -> 는 앞자리의 1의 값들을 빼준 값임) | |||||||||
숫자 | index | 숫자 | index | 숫자 | index | 숫자 | index | 숫자 | index |
1 | 1 | 11 | 2 -> 1 | 111 | 3 -> 1 | 1111 | 4 -> 1 | 11111 | 5 -> 1 |
2 | 782 | 12 | 158 -> 157 | 112 | 34 -> 32 | 1112 | 10 -> 7 | 11112 | 6 -> 2 |
3 | 1563 | 13 | 314 -> 313 | 113 | 65 -> 63 | 1113 | 16 -> 13 | 11113 | 7 -> 3 |
4 | 2344 | 14 | 470 -> 469 | 114 | 96 -> 94 | 1114 | 22 -> 19 | 11114 | 8 -> 4 |
5 | 3125 | 15 | 626 -> 625 | 115 | 127 -> 125 | 1115 | 28 -> 25 | 11115 | 9 -> 5 |
1 + 781(n-1) | 1 + 156(n - 1) | 1 + 31(n-1) | 1 + 6(n-1) | 1 + (n -1) | |||||
1 + (625 + 125 + 25 +1) * (n - 1) | 1 + (125 + 25 + 1) * (n - 1) | 1 + (25 + 5 + 1) * (n - 1) | 1 + (5 + 1) * (n - 1) | 1 + (n - 1) | |||||
1 + (5^5 - 1) / (5-1) * (n-1) | 1 + (5^4 - 1)/(5-1) * (n-1) | 1 + (5^3 - 1)/(5-1) * (n-1) | 1 + (5^2 - 1)/(5-1)*(n-1) | 1 + (5^1 - 1)/(5-1) * (n-1) |
** Javascript
1) 제출한 풀이
+1점을 받았다... ㅋㅋㅋ.... 슬픔.. 근데 다시 보니 그럴 만하다. 그래도 풀었다는 점이 중요하다.
function solution(word) {
const vowels = ['A', 'E', 'I', 'O', 'U'];
return Array.from(word).reduce((acc, w, index) => {
const n = vowels.indexOf(w);
switch(index) {
case 0:
acc += (n === 0) ? 1 : 781 * n + 1;
break;
case 1:
acc += (n === 0) ? 1 : 156 * n + 1;
break;
case 2:
acc += (n === 0) ? 1 : 31 * n + 1;
break;
case 3:
acc += (n === 0) ? 1 : 6 * n + 1;
break;
case 4:
acc += (n === 0) ? 1 : n + 1;
break;
default:
break;
}
return acc;
}, 0);
}
2) 리팩토링 1차
function solution(word) {
const vowels = ['A', 'E', 'I', 'O', 'U'];
return Array.from(word).reduce((acc, w, index) => {
const n = vowels.indexOf(w);
switch(index) {
case 0:
acc += 781 * n + 1; // 625 + 125 + 25 + 1
break;
case 1:
acc += 156 * n + 1; // 125 + 25 + 5 + 1
break;
case 2:
acc += 31 * n + 1; // 25 + 5 + 1
break;
case 3:
acc += 6 * n + 1; // 5 + 1
break;
case 4:
acc += n + 1; // 1
break;
default:
break;
}
return acc;
}, 0);
}
옆에 주석은 내가 간과했던 규칙이다. 등비수열의 합
공비가 5인 점만 이용하면 된다.
3) 리팩토링 2차
function solution(word) {
const vowels = ['A', 'E', 'I', 'O', 'U'];
return Array.from(word).reduce((acc, w, index) => {
const n = vowels.indexOf(w);
acc += (5 ** (5 - index) - 1) / (5 - 1) * n + 1
return acc;
}, 0);
}
4) 리팩토링 3차
function solution(word) {
const vowels = ['A', 'E', 'I', 'O', 'U'];
return Array.from(word).reduce((acc, w, index) => acc += (5 ** (5 - index) - 1) / (5 - 1) * vowels.indexOf(w) + 1, 0);
}
5) 리팩토링 4차
function solution(word) {
return Array.from(word).reduce((acc, w, index) => acc += (5 ** (5 - index) - 1) / (5 - 1) * "AEIOU".indexOf(w) + 1, 0);
}
** Java
import java.lang.Math;
class Solution {
public int solution(String word) {
String[] words = word.split("");
int answer = 0;
for (int i = 0; i < word.length(); i++) {
answer += (Math.pow(5, 5 - i) - 1) / (5 - 1) * "AEIOU".indexOf(words[i]) + 1;
}
return answer;
}
}
** Python
def solution(word):
answer = 0
for i, n in enumerate(word):
answer += (5 ** (5 - i) - 1) / (5 - 1) * "AEIOU".index(n) + 1
return answer
반응형
'프로그래머스 > level 2' 카테고리의 다른 글
[프로그래머스] 숫자의 표현 (0) | 2021.12.06 |
---|---|
[프로그래머스] 행렬의 곱셈 (0) | 2021.11.29 |
[프로그래머스] Jaden Case 문자열 만들기 (0) | 2021.08.14 |
[프로그래머스] H-Index (0) | 2021.08.11 |
[프로그래머스] 최댓값과 최솟값 (0) | 2021.08.10 |