Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// https://school.programmers.co.kr/learn/courses/30/lessons/92335

//! ver 1. 최초 코드
function primeCnt(n: number, k: number) {
// 소수 판별 함수
const isPrime = (num: number) => {
if (num === 1) return false;
for (let i = 2; i * i <= num; i++) if (num % i === 0) return false;
return true;
};

const convertedNum = n.toString(k); // k진수로 변환
const splitedConvertedNum = convertedNum.split('0'); // 조건에 맞게 분리

const ans = splitedConvertedNum.filter((v) => {
if (v === '') return false;
if (isPrime(parseInt(v, 10))) return true;
else return false;
});
return ans.length;
}

//! ver 2. 개선 코드
// filter 함수의 콜백 함수를 리펙토링함
const primeCnt2 = (n: number, k: number) => {
// 소수 판별 함수
const isPrime = (num: number) => {
if (num === 1) return false;
for (let i = 2; i * i <= num; i++) if (num % i === 0) return false;
return true;
};
// 코어 로직
const convertedNum = n.toString(k); // k진수로 변환
const splitedConvertedNum = convertedNum.split('0'); // 조건에 맞게 분리
return splitedConvertedNum.filter((v) => isPrime(parseInt(v, 10)) && v !== '').length;
};

console.log(primeCnt(437674, 3)); // 3
console.log(primeCnt(110011, 10)); // 2
console.log(primeCnt2(437674, 3)); // 3
console.log(primeCnt2(110011, 10)); // 2
22 changes: 22 additions & 0 deletions 2.coding-test/programmers/sprint-1-test/qrcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// https://school.programmers.co.kr/learn/courses/30/lessons/181903

//! ver 1. 최초 코드
function restIdxString(q, r, code) {
return [...code]
.filter((v, i) => {
if (i % q === r) return true;
})
.join('');
}

//! ver 2. 개선 코드
// 불필요한 if문 제거
const restIdxString2 = (q, r, code) => {
return [...code].filter((_, i) => i % q === r).join('');
};

//| 입출력 예
console.log(restIdxString(3, 1, 'qjnwezgrpirldywt')); // "jerry"
console.log(restIdxString(1, 0, 'programmers')); // "programmers"
console.log(restIdxString2(3, 1, 'qjnwezgrpirldywt')); // "jerry"
console.log(restIdxString2(1, 0, 'programmers')); // "programmers"
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 마라톤
// 한 명만 미완주

//! ver 1. 최초 코드
function dnfPerson(participant, completion) {
// 동명 이인 없을 경우 => 시간 효율 측면에서 불필요
// const firstLoop = participant.filter(name => {
// if(!completion.some(v=> v === name)) return true
// })
// if(firstLoop.length !== 0) return firstLoop.at(0)

// 동명이인도 있는 경우
const prtMap = new Map();
const comMap = new Map();
participant.forEach((name) => prtMap.set(name, (prtMap.get(name) ?? 0) + 1));
completion.forEach((name) => comMap.set(name, (comMap.get(name) ?? 0) + 1));

return participant
.filter((name) => {
if (prtMap.get(name) !== comMap.get(name)) return true;
})
.at(0);
}

//! ver 2. 개선 코드
// filter의 콜백 함수 단순화
const dnfPerson2 = (participant, completion) => {
const prtMap = new Map();
const comMap = new Map();
participant.forEach((name) => prtMap.set(name, (prtMap.get(name) ?? 0) + 1));
completion.forEach((name) => comMap.set(name, (comMap.get(name) ?? 0) + 1));

// 코어 로직 한 줄 정리
return participant.filter((name) => prtMap.get(name) !== comMap.get(name)).at(0);
};

console.log(dnfPerson(['leo', 'kiki', 'eden'], ['eden', 'kiki'])); // "leo"
console.log(dnfPerson(['marina', 'josipa', 'nikola', 'vinko', 'filipa'], ['josipa', 'filipa', 'marina', 'nikola'])); // "vinko"
console.log(dnfPerson(['mislav', 'stanko', 'mislav', 'ana'], ['stanko', 'ana', 'mislav'])); // "mislav"

console.log(dnfPerson2(['leo', 'kiki', 'eden'], ['eden', 'kiki'])); // "leo"
console.log(dnfPerson2(['marina', 'josipa', 'nikola', 'vinko', 'filipa'], ['josipa', 'filipa', 'marina', 'nikola'])); // "vinko"
console.log(dnfPerson2(['mislav', 'stanko', 'mislav', 'ana'], ['stanko', 'ana', 'mislav'])); // "mislav"
21 changes: 21 additions & 0 deletions 2.coding-test/programmers/sprint-1-test/체육복.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://school.programmers.co.kr/learn/courses/30/lessons/42862

//! ver 1. 최초 코드
// 정답률 90%
Comment on lines +3 to +4
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 풀어보기

function 체육복(n, lost, reserve) {
const finalLost = lost.filter((v) => {
if (reserve.some((e) => e === v)) {
reserve.splice(reserve.indexOf(v), 1);
} else if (reserve.some((e) => e === v - 1)) {
reserve.splice(reserve.indexOf(v - 1), 1);
} else if (reserve.some((e) => e === v + 1)) {
reserve.splice(reserve.indexOf(v + 1), 1);
} else return true;
});
return n - finalLost.length;
}

//| 입출력 예
console.log(체육복(5, [2, 4], [1, 3, 5])); // 5
console.log(체육복(5, [2, 4], [3])); // 4
console.log(체육복(3, [3], [1])); // 2
19 changes: 19 additions & 0 deletions 2.coding-test/programmers/sprint-1-test/피로도-던전.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://school.programmers.co.kr/learn/courses/30/lessons/87946

// 탐험 시작 위한 `최소 필요 피로도`
// 탐험 끝났을 때 소모되는 `소모 피로도`

//! ver 1. 최초 코드 - 못 품
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 풀어보기

function maxDungeon(k, dungeons) {
dungeons.sort((a, b) => b[0] - b[1] - (a[0] - a[1]));
const a = dungeons.filter((v) => {
k - v[1];
if (k >= 0) return true;
});
console.log(dungeons);
console.log(a);
return a.length;
}

//! ver 2. 최초 코드 - 재풀이
const maxDungeon2 = (k, dungeons) => {};