Skip to content

프로그래머스 0단계 #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: haejung
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions 프로그래머스/0단계/a와b출력하기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

function printFormat(a, b) {
console.log(`a = ${a}\nb = ${b}`);
}

let input = [];

rl.on("line", function (line) {
input = line.split(" ").map(Number);
printFormat(input[0], input[1]);
rl.close();
});
13 changes: 13 additions & 0 deletions 프로그래머스/0단계/구슬을나누는경우의수.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function solution(balls, share) {
if (share > balls) {
return 0;
}

let answer = 1;
for (let i = 1; i <= share; i++) {
answer *= balls - i + 1;
answer /= i;
}

return answer;
}
14 changes: 14 additions & 0 deletions 프로그래머스/0단계/그림확대.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function solution(picture, k) {
const answer = [];
for (let i = 0; i < picture.length; i++) {
const row = picture[i];
const enlargedRow = row
.split("")
.map((char) => char.repeat(k))
.join("");
for (let j = 0; j < k; j++) {
answer.push(enlargedRow);
}
}
return answer;
}
5 changes: 5 additions & 0 deletions 프로그래머스/0단계/두수의합.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function solution(a, b) {
const sum = BigInt(a) + BigInt(b);

return sum.toString();
}
21 changes: 21 additions & 0 deletions 프로그래머스/0단계/로그인성공.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function solution(id_pw, db) {
const [inputId, inputPw] = id_pw;
let idMatch = false;

for (let i = 0; i < db.length; i++) {
const [dbId, dbPw] = db[i];

if (inputId === dbId) {
idMatch = true;
if (inputPw === dbPw) {
return "login";
} else {
return "wrong pw";
}
}
}

if (!idMatch) {
return "fail";
}
}
15 changes: 15 additions & 0 deletions 프로그래머스/0단계/무작위로k개의수뽑기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function solution(arr, k) {
const uniqueSet = new Set();

for (const num of arr) {
uniqueSet.add(num);
}

const uniqueArr = [...uniqueSet];

while (uniqueArr.length < k) {
uniqueArr.push(-1);
}

return uniqueArr.slice(0, k);
}
5 changes: 5 additions & 0 deletions 프로그래머스/0단계/문자열겹쳐쓰기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function solution(my_string, overwrite_string, s) {
const start = my_string.slice(0, s);
const end = my_string.slice(s + overwrite_string.length);
return start + overwrite_string + end;
}
16 changes: 16 additions & 0 deletions 프로그래머스/0단계/문자열계산하기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function solution(my_string) {
const string = my_string.split(" ");
let answer = parseInt(string[0]);

for (let i = 1; i < string.length; i += 2) {
const op = string[i];
const operand = parseInt(string[i + 1]);
if (op === "+") {
answer += operand;
} else if (op === "-") {
answer -= operand;
}
}

return answer;
}
13 changes: 13 additions & 0 deletions 프로그래머스/0단계/문자열여러번뒤집기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function solution(my_string, queries) {
for (let i = 0; i < queries.length; i++) {
const [start, end] = queries[i];
const reverse = my_string
.substring(start, end + 1)
.split("")
.reverse()
.join("");
my_string =
my_string.substring(0, start) + reverse + my_string.substring(end + 1);
}
return my_string;
}
21 changes: 21 additions & 0 deletions 프로그래머스/0단계/배열만들기6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function solution(arr) {
const stk = [];
let i = 0;

while (i < arr.length) {
if (stk.length === 0) {
stk.push(arr[i]);
i++;
} else {
if (stk[stk.length - 1] === arr[i]) {
stk.pop();
i++;
} else {
stk.push(arr[i]);
i++;
}
}
}

return stk.length > 0 ? stk : [-1];
}
8 changes: 8 additions & 0 deletions 프로그래머스/0단계/삼각형의완성조건(2).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function solution(sides) {
const [a, b] = sides.sort((a, b) => a - b);

const minPossibleSide = b - a + 1;
const maxPossibleSide = a + b - 1;

return maxPossibleSide - minPossibleSide + 1;
}
21 changes: 21 additions & 0 deletions 프로그래머스/0단계/수열과구간쿼리2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function solution(arr, queries) {
const result = [];

for (const query of queries) {
const [start, end, k] = query;

const sortedSubArr = arr.slice(start, end + 1).sort((a, b) => a - b);
let minGreater = -1;

for (const num of sortedSubArr) {
if (num > k) {
minGreater = num;
break;
}
}

result.push(minGreater);
}

return result;
}
28 changes: 28 additions & 0 deletions 프로그래머스/0단계/영어가싫어요.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function solution(numbers) {
const number = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};

let result = "";
let currentWord = "";

for (let i = 0; i < numbers.length; i++) {
currentWord += numbers[i];

if (number.hasOwnProperty(currentWord)) {
result += number[currentWord];
currentWord = "";
}
}

return parseInt(result);
}
12 changes: 12 additions & 0 deletions 프로그래머스/0단계/외계어사전.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(spell, dic) {
const spellWord = spell.sort().join("");

for (let i = 0; i < dic.length; i++) {
const dicWord = dic[i].split("").sort().join("");
if (spellWord === dicWord) {
return 1;
}
}

return 2;
}
16 changes: 16 additions & 0 deletions 프로그래머스/0단계/왼쪽오른쪽.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function solution(str_list) {
let leftIndex = str_list.indexOf("l");
let rightIndex = str_list.indexOf("r");

if (leftIndex === -1 && rightIndex === -1) {
return [];
} else if (leftIndex !== -1 && rightIndex !== -1) {
return leftIndex < rightIndex
? str_list.slice(0, leftIndex)
: str_list.slice(rightIndex + 1);
} else if (leftIndex !== -1) {
return str_list.slice(0, leftIndex);
} else {
return str_list.slice(rightIndex + 1);
}
}
19 changes: 19 additions & 0 deletions 프로그래머스/0단계/정사각형으로_만들기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function solution(arr) {
const rowCount = arr.length;
const colCount = arr[0].length;

if (rowCount > colCount) {
for (let i = 0; i < rowCount; i++) {
while (arr[i].length < rowCount) {
arr[i].push(0);
}
}
} else if (colCount > rowCount) {
while (arr.length < colCount) {
const newRow = new Array(colCount).fill(0);
arr.push(newRow);
}
}

return arr;
}
12 changes: 12 additions & 0 deletions 프로그래머스/0단계/조건문자열.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(ineq, eq, n, m) {
if (
(ineq === "<" && eq === "=" && n <= m) ||
(ineq === ">" && eq === "=" && n >= m) ||
(ineq === "<" && eq === "!" && n < m) ||
(ineq === ">" && eq === "!" && n > m)
) {
return 1;
} else {
return 0;
}
}
3 changes: 3 additions & 0 deletions 프로그래머스/0단계/종이자르기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(M, N) {
return M * N - 1;
}
9 changes: 9 additions & 0 deletions 프로그래머스/0단계/직사각형_넓이구하기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function solution(dots) {
const xCoords = dots.map((dot) => dot[0]);
const yCoords = dots.map((dot) => dot[1]);

const width = Math.max(...xCoords) - Math.min(...xCoords);
const height = Math.max(...yCoords) - Math.min(...yCoords);

return width * height;
}
24 changes: 24 additions & 0 deletions 프로그래머스/0단계/캐릭터의좌표.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function solution(keyinput, board) {
let position = [0, 0];
const maxX = Math.floor(board[0] / 2);
const maxY = Math.floor(board[1] / 2);

keyinput.forEach((key) => {
switch (key) {
case "up":
if (position[1] < maxY) position[1] += 1;
break;
case "down":
if (position[1] > -maxY) position[1] -= 1;
break;
case "left":
if (position[0] > -maxX) position[0] -= 1;
break;
case "right":
if (position[0] < maxX) position[0] += 1;
break;
}
});

return position;
}