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
24 changes: 24 additions & 0 deletions k/programmers/구현/Lv2_오픈채팅방_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function solution(record) {
const userInfo = {};
const ans = [];

// 유저 정보
record.map((item) => {
const [command, uid, nickname] = item.split(' ');

if (command === 'Enter') {
userInfo[uid] = nickname;
ans.push(`${uid}님이 들어왔습니다.`);
} else if (command === 'Change') {
userInfo[uid] = nickname;
} else if (command === 'Leave') {
ans.push(`${uid}님이 나갔습니다.`);
}
});
// 입장, 퇴장 기록
for (let i=0; i < ans.length; i++) {
let uid = ans[i].split('님')[0];
ans[i] = ans[i].replace(uid, userInfo[uid]);
}
return ans;
}
42 changes: 42 additions & 0 deletions k/programmers/구현/Lv2_주차 요금 계산_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function solution(fees, records) {
const answer = [];
const carIn = {};
let carOut = {};
const [baseT, baseF, unitT, unitF] = fees;

// 시간 계산
records.map((item) => {
const [clock, num, record] = item.split(' ');
const [hour, min] = clock.split(':');
const time = parseInt(hour) * 60 + parseInt(min);

if (record === 'IN') {
carIn[num] = time;
} else {
carOut.hasOwnProperty(num)
? carOut[num] = carOut[num] + time - carIn[num]
: carOut[num] = time - carIn[num];
delete carIn[num];
}
});
// 오늘 출차하지 않은 차량
for (const [num, time] of Object.entries(carIn)) {
carOut.hasOwnProperty(num)
? carOut[num] = carOut[num] + 1439 - time
: carOut[num] = 1439 - time;
delete carIn[num];
}
// 차량번호 오름차순 정렬
carOut = Object.entries(carOut).sort((a,b) => a[0] - b[0]);

// 요금 계산
carOut.map((item) => {
const time = item[1];
answer.push(
time <= baseT
? baseF
: baseF + Math.ceil((time - baseT) / unitT) * unitF
);
})
return answer;
}
29 changes: 29 additions & 0 deletions k/programmers/문자열/Lv1_다트 게임_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function solution(dartResult) {
const answer = [];
let tmp;

for (let i=0; i < dartResult.length; i++) {
let item = dartResult[i];

if (!isNaN(Number(item))) { // 정수라면
tmp = dartResult[i-1] == 1 ? 10 : Number(item);
} else {
switch (item) {
case 'S': answer.push(tmp);
break;
case 'D': answer.push(Math.pow(tmp, 2));
break;
case 'T': answer.push(Math.pow(tmp, 3));
break;
case '*':
answer[answer.length-2] = answer[answer.length-2] * 2;
answer[answer.length-1] = answer[answer.length-1] * 2;
break;
case '#':
answer[answer.length-1] = answer[answer.length-1] * (-1);
break;
}
}
}
return answer.reduce((acc, cur) => acc + cur, 0);
}
22 changes: 22 additions & 0 deletions k/programmers/문자열/Lv2_압축_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function solution(msg) {
const alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'];
let tmp = '';
const answer = [];

for (let i=0; i < msg.length; i++) {
tmp += msg[i];

if (!alphabet.includes(tmp)) {
answer.push(alphabet.indexOf(tmp.slice(0, -1)) +1); // 색인 번호
alphabet.push(tmp);
tmp = msg[i]; // 현재 시점
}
}
if (tmp) {
answer.push(alphabet.indexOf(tmp) +1)
}
return answer;
}
16 changes: 16 additions & 0 deletions k/programmers/반복문/Lv2_n진수 게임_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function solution(n, t, m, p) {
let num = '';
let ans = '';

for (let i = 0; num.length < m * t; i++) {
num += i.toString(n).toUpperCase();
}
num.split('').map((item, idx) => {
if (idx % m === (p-1)) {
if (ans.length < t) {
ans += item;
}
}
});
return ans;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function solution(n, k) {
let count = 0;
n.toString(k).split("0")
.map((num) => {
if (isPrime(Number(num))) {
count++;
}
});
return count;
}

function isPrime(num) {
if (num <= 1) {
return false;
}
for (let i=2; i < Math.sqrt(num); i++) {
if (num % i === 0) { // 약수가 있다면 소수가 아니다
return false;
}
}
return true;
}