function solution(dartResult) {
let origin = [...dartResult].reduce((arr,v,i)=>{
if (v==='0' && arr[i-1]==='1') return [...arr,'10'];
if (v==='1' && arr[i+1]==='0') return [...arr];
return [...arr,v];
},[]);
let scores = [];
origin.forEach((v,i)=>{
const len = scores.length;
if (v==='S') scores.push(origin[i-1]/1);
if (v==='D') scores.push(Math.pow(origin[i-1]/1 , 2));
if (v==='T') scores.push(Math.pow(origin[i-1]/1 , 3));
if (v==='*') {
const hasBefore = len >= 2;
if(hasBefore) scores[len-2] = scores[len-2]*2;
scores[len-1] = scores[len-1]*2;
}
if (v==='#') scores[len-1] = scores[len-1] - 2*scores[len-1];
})
return scores.reduce((accum,v)=>accum+v,0);
}
function solution(n, arr1, arr2) {
const toBinary = (arr) => arr.map(item=>item.toString(2))
.map(str=> '0'.repeat(n - str.length)+str)
.map(binary => Array.from(binary).map(digit => digit/1));
const binaryMap1 = toBinary(arr1);
const binaryMap2 = toBinary(arr2);
const resultMap = binaryMap1.map((line,index) => line.map((d,i)=> binaryMap2[index][i] + d >= 1? 1: 0 ));
return resultMap.reduce((arr,line)=> [...arr, line.map(digit=> digit? '#':' ').join('')],[]);
}
toBinary는 10진수인 배열요소를 2진수로 만든 뒤, 부족한 만큼 앞자리에 0을 넣어 숫자의 길이를 맞춰준다.
그런 뒤, Array.from() 메서드로 문자열을 배열로 만들고, 각 요소를 정수로 변경한다.
resultMap 은 이런 형태가 된다.
[ [ 1, 1, 1, 1, 1 ],
[ 1, 0, 1, 0, 1 ],
[ 1, 1, 1, 0, 1 ],
[ 1, 0, 0, 1, 1 ],
[ 1, 1, 1, 1, 1 ] ]
reduce() 메서드를 이용해 문제의 결과에 맞게 형태를 변경해준다.
function solution(N, stages) {
let divisors = Array(N+1).fill(0);
stages.forEach((stage)=>{
for (let i = 0; i < stage; i++) {divisors[i] += 1}
})
const Rate = divisors.reduce((arr,divisor,index)=>{
return [...arr, divisor === 0? 0 :stages.filter(stage => stage===index+1).length/divisor]
},[]);
const result = Rate.reduce((arr,rate,index)=>[...arr ,[index+1 , rate]],[]);
result.pop();
result.sort((a,b)=> {
if(a[1]===b[1]) return a[0]-b[0];
return b[1]-a[1];
})
return result.map(v => v[0]);
}
function solution(d, budget) {
let supports = 0;
d.sort((a,b)=>a-b).reduce((accum,v) => {
if (accum+v <= budget) supports++;
return accum+v;
},0);
return supports;
}
function solution(x, n) {
const array = Array(n).fill(x);
return array.reduce((arr,v,i)=>{
const num = arr[i-1] ? arr[i-1] + x : x;
return [...arr,num];
},[]);
}
function solution(arr1, arr2) {
return arr1.map((arr,index)=>arr.map((v,i)=>v+arr2[index][i]));
}
function solution(phone_number) {
return [...phone_number].map((v,i)=> i<=[...phone_number].length-5? '*':v).join('');
}
function solution(x) {
return x % Array.from(x+'').map(i => i/1).reduce((acc,i)=> acc+i,0) ? false : true;
}
function solution(num) {
let step = 0;
let n = num;
while (n > 1) {
let isOdd = n % 2 === 0? false : true;
n = isOdd? n * 3 + 1 : n / 2;
step++;
if(step==500) {
step = -1;
break;
};
}
return step;
}
function solution(n, m) {
const gcd = (a,b) => b? gcd(b, a%b) : a;
return [gcd(n,m) , n*m /gcd(n,m)];
}
function solution(arr) {
arr.splice(arr.findIndex(v=> v===Math.min(...arr)),1)
return arr.length <=1? [-1] : arr;
}
function solution(n) {
const sqrt = Math.sqrt(n) + 1;
return Number.isInteger(sqrt)? Math.pow(sqrt,2) : -1;
}
function solution (s) {
const result = s.toUpperCase().split(' ').map(item => {
return Array.from(item).map((a,i)=> i%2===1? a.toLowerCase() : a);
})
return [...result].join(' ').split(',').join('');
}
function solution(n) {
let result = 0
for (let i = 1; i <= n; i++) {
if (n % i == 0) result = result + i;
}
return result;
}
function solution(s, n) {
const lower = [];
for(let i = 97; i<= 122; i++) {lower.push(i);};
const upper = lower.map(item=> item-32);
const findIndex = (alpha, n) => {
const ascii = alpha.charCodeAt(0);
const a = lower.find(i => i===ascii);
const A = upper.find(i => i===ascii);
let encoded;
if(!a && !A) return ' ';
if(a) encoded = a+n>122? a-26+n : a+n;
if(A) encoded = A+n>90? A-26+n : A+n;
return String.fromCharCode(encoded);
}
const result = Array.from(s).map(alpha => {
let encoded = findIndex(alpha,n);
return encoded;
})
return result.join('');
}
function solution(n) {
return Array(n).fill('').map((item,index)=> index % 2 === 0? '수':'박').join('');
}
function solution(n) {
const sqrt = Math.sqrt(n);
let primes = Array(n).fill(1);
let count = 2;
while(count <= sqrt){
for(let a=count+count; a<=n; a+=count) {
primes[a-1] = 0;
}
count++;
}
primes[0] = 0;
return primes.filter(v=>v>=1).length;
}
function solution(s){
const result = Array.from(s).map(item=>{
if(item === 'p' || item ==='P') return -1;
if(item === 'y' || item ==='Y') return 1;
return 0;
}).reduce((ac,cr)=>ac+cr,0);
return result===0
}
function solution(s) {
return s.match(/p/ig).length == s.match(/y/ig).length;
}
function solution(strings, n) {
strings.sort((a,b) => {
if(a[n] > b[n]) return 1;
if(a[n] < b[n]) return -1;
if(a[n] === b[n]) return a.localeCompare(b);
})
return strings;
}
function solution(a, b) {
return (a + b) * (Math.abs(a-b)+1)/2;
}
function solution(arr, divisor) {
const divided = arr.map(item=>{
return item % divisor === 0 ? item : undefined;
}).filter(i => i).sort((a,b)=>a-b);
return divided.length===0? [-1] : divided;
}
function solution(arr){
return arr.map((item,index)=>item===arr[index+1]? undefined : item).filter(i=> i>=0);
}
function solution(a, b) {
const date = new Date(2016,a-1,b);
const days = ['SUN','MON','TUE','WED','THU','FRI','SAT'];
return days[date.getDay()];
}
function solution(array, commands) {
return commands.map(command => {
let i = command[0]-1;
let j = command[1];
let k = command[2]-1;
return [...array].slice(i,j).sort((a,b)=>a-b)[k];
});
}
function solution(n, lost, reserve) {
let students = Array(n).fill(1);
reserve.forEach(a => students[a-1]=2);
lost.forEach(a=> students[a-1]-- );
students.forEach((item,i)=>{
if(item>=2 && students[i-1]===0){
students[i-1] = 1;
students[i] = 1;
}
})
students.forEach((item,i)=>{
if(item>=2 && students[i+1]===0 ){
students[i+1] = 1;
students[i] = 1;
}
})
return students.filter(i=> i>0).length
}
function solution(answers) {
const length = answers.length;
const patternA = [1,2,3,4,5].join('').toString().repeat(Math.ceil(length/5)).split('').map(i=>i/1);
const patternB = [2,1,2,3,2,4,2,5].join('').toString().repeat(Math.ceil(length/8)).split('').map(i=>i/1);
const patternC = [3,3,1,1,2,2,4,4,5,5].join('').toString().repeat(Math.ceil(length/10)).split('').map(i=>i/1);
const score = [[1,0],[2,0],[3,0]];
answers.forEach((item,index)=>{
if(patternA[index]===item) score[0][1]++;
if(patternB[index]===item) score[1][1]++;
if(patternC[index]===item) score[2][1]++;
})
return score.filter(item=>item[1]=== Math.max(...score.map(item=>item[1])))
.map(item=>item[0]).sort();
}
function solution(participant, completion) {
participant.sort();
completion.sort();
let result;
participant.some((element,index)=>{
if(element !== completion[index]) {
result = element
return true
}
})
return result;
}
function solution (board, moves) {
const basket = [];
let result = 0;
moves.forEach((move,i)=>{
const foundLine = board.find(line=> line[move-1] > 0);
if (foundLine) {
basket.push(foundLine[move-1]);
foundLine[move-1] = 0;
}
const index = basket.findIndex((v,i)=> v=== basket[i+1]);
if (index > -1) {
basket.splice(index,1);
basket.splice(index,1);
result++;
}
})
return result*2;
}
몇몇 문제는 제외했습니다.
읽어주셔서 감사합니다.