Skip to content
Merged
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
16 changes: 16 additions & 0 deletions level-01/1.1-사칙연산/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.1 사칙연산", () => {
it("기본 사칙연산", () => {
expect(solution(7, 2)).toEqual({ add: 9, sub: 5, mul: 14, div: 3 });
});

it("음수가 섞인 경우 몫은 0 방향으로 버린다", () => {
expect(solution(-7, 2)).toEqual({ add: -5, sub: -9, mul: -14, div: -3 });
});

it("나누어떨어지는 경우", () => {
expect(solution(10, 5)).toEqual({ add: 15, sub: 5, mul: 50, div: 2 });
});
});
24 changes: 24 additions & 0 deletions level-01/1.1-사칙연산/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 1.1 사칙연산 [프로그래머스 Lv0 / 백준 Bronze V]
*
* 두 정수 a, b의 합(add)·차(sub)·곱(mul)·몫(div)을 객체로 반환하세요.
* 몫은 0 방향으로 버림한 정수입니다. b는 0이 아님이 보장됩니다.
*
* @example
* solution(7, 2) // => { add: 9, sub: 5, mul: 14, div: 3 }
*/
export interface ArithmeticResult {
add: number;
sub: number;
mul: number;
div: number;
}

export function solution(a: number, b: number): ArithmeticResult {
return {
add: a + b,
sub: a - b,
mul: a * b,
div: Math.trunc(a / b)
}
}
24 changes: 24 additions & 0 deletions level-01/1.2-홀짝판별/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.2 홀짝 판별", () => {
it("홀수를 판별한다", () => {
expect(solution(3)).toBe("홀수");
});

it("짝수를 판별한다", () => {
expect(solution(4)).toBe("짝수");
});

it("0은 짝수로 판별한다", () => {
expect(solution(0)).toBe("짝수");
});

it("음수 홀수도 판별한다", () => {
expect(solution(-7)).toBe("홀수");
});

it("음수 짝수도 판별한다", () => {
expect(solution(-4)).toBe("짝수");
});
});
11 changes: 11 additions & 0 deletions level-01/1.2-홀짝판별/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 1.2 홀짝 판별 [프로그래머스 Lv0 / 백준 Bronze V]
*
* 정수 n이 홀수면 "홀수", 짝수면 "짝수"를 반환하세요. (0은 짝수)
*
* @example
* solution(3) // => "홀수"
*/
export function solution(n: number): "홀수" | "짝수" {
return n % 2 === 0 ? "짝수" : "홀수";
}
16 changes: 16 additions & 0 deletions level-01/1.3-세수최댓값/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.3 세 수의 최댓값", () => {
it("일반적인 경우", () => {
expect(solution(3, 7, 5)).toBe(7);
});

it("모두 음수", () => {
expect(solution(-1, -9, -3)).toBe(-1);
});

it("같은 값이 섞인 경우", () => {
expect(solution(5, 5, 2)).toBe(5);
});
});
11 changes: 11 additions & 0 deletions level-01/1.3-세수최댓값/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 1.3 세 수의 최댓값 [프로그래머스 Lv0 / 백준 Bronze V]
*
* 세 정수 a, b, c 중 가장 큰 값을 반환하세요.
*
* @example
* solution(3, 7, 5) // => 7
*/
export function solution(a: number, b: number, c: number): number {
return Math.max(...[a, b, c]);
}
31 changes: 31 additions & 0 deletions level-01/1.4-점수등급/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.4 점수 등급", () => {
it("구간 가운데 점수의 등급을 반환한다", () => {
expect(solution(95)).toBe("A");
expect(solution(85)).toBe("B");
expect(solution(73)).toBe("C");
expect(solution(65)).toBe("D");
expect(solution(40)).toBe("F");
});

it("경계값은 위 등급에 속한다", () => {
expect(solution(90)).toBe("A");
expect(solution(80)).toBe("B");
expect(solution(70)).toBe("C");
expect(solution(60)).toBe("D");
});

it("경계 바로 아래는 아래 등급에 속한다", () => {
expect(solution(89)).toBe("B");
expect(solution(79)).toBe("C");
expect(solution(69)).toBe("D");
expect(solution(59)).toBe("F");
});

it("최고점 100은 A, 최저점 0은 F다", () => {
expect(solution(100)).toBe("A");
expect(solution(0)).toBe("F");
});
});
12 changes: 12 additions & 0 deletions level-01/1.4-점수등급/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 1.4 점수 등급 [프로그래머스 Lv0 / 백준 Bronze V]
*
* 시험 점수(0~100)를 받아 등급을 반환하세요.
* 90 이상 "A", 80 이상 "B", 70 이상 "C", 60 이상 "D", 그 외 "F"입니다.
*
* @example
* solution(73) // => "C"
*/
export function solution(score: number): "A" | "B" | "C" | "D" | "F" {
return score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : score >= 60 ? "D" : "F"
}
20 changes: 20 additions & 0 deletions level-01/1.5-윤년판별/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.5 윤년 판별", () => {
it("400의 배수는 윤년", () => {
expect(solution(2000)).toBe(true);
});

it("100의 배수지만 400의 배수가 아니면 평년", () => {
expect(solution(1900)).toBe(false);
});

it("4의 배수 윤년", () => {
expect(solution(2024)).toBe(true);
});

it("4의 배수가 아니면 평년", () => {
expect(solution(2023)).toBe(false);
});
});
13 changes: 13 additions & 0 deletions level-01/1.5-윤년판별/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* 1.5 윤년 판별 [프로그래머스 Lv0 / 백준 Bronze V]
*
* 연도 year가 윤년이면 true, 아니면 false를 반환하세요.
* 윤년은 4의 배수이면서, 100의 배수가 아니거나 400의 배수인 해입니다.
*
* @example
* solution(2024) // => true
* solution(1900) // => false
*/
export function solution(year: number): boolean {
return new Date(year, 2, 0).getDate() === 29;
}
20 changes: 20 additions & 0 deletions level-01/1.6-초를시분초로/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.6 초를 시:분:초로", () => {
it("3661초", () => {
expect(solution(3661)).toBe("01:01:01");
});

it("0을 두 자리로 채운다", () => {
expect(solution(59)).toBe("00:00:59");
});

it("0초", () => {
expect(solution(0)).toBe("00:00:00");
});

it("24시간을 넘어가는 경우", () => {
expect(solution(90000)).toBe("25:00:00");
});
});
17 changes: 17 additions & 0 deletions level-01/1.6-초를시분초로/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 1.6 초를 시:분:초로 [프로그래머스 Lv0 / 백준 Bronze IV]
*
* 0 이상의 정수 sec(초)를 "HH:MM:SS" 형식 문자열로 반환하세요.
* 시·분·초는 항상 두 자리로 0을 채우고, 시는 24를 넘어갈 수 있습니다.
*
* @example
* solution(3661) // => "01:01:01"
*/
export function solution(sec: number): string {
const HH = Math.floor(sec / 3600);
const MM = Math.floor((sec % 3600) / 60);
const SS = sec % 60;
const pad = (num :any) => String(num).padStart(2, "0");

return `${pad(HH)}:${pad(MM)}:${pad(SS)}`;
}
16 changes: 16 additions & 0 deletions level-01/1.7-부호분류/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.7 부호 분류", () => {
it("양수/음수/0을 센다", () => {
expect(solution([1, -2, 0, 3, 0, -5])).toEqual({
positive: 2,
negative: 2,
zero: 2,
});
});

it("빈 배열은 모두 0", () => {
expect(solution([])).toEqual({ positive: 0, negative: 0, zero: 0 });
});
});
22 changes: 22 additions & 0 deletions level-01/1.7-부호분류/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 1.7 부호 분류 [프로그래머스 Lv0 / 백준 Bronze IV]
*
* 정수 배열을 받아 양수·음수·0의 개수를 객체로 반환하세요.
*
* @example
* solution([1, -2, 0, 3, 0, -5])
* // => { positive: 2, negative: 2, zero: 2 }
*/
export interface SignCount {
positive: number;
negative: number;
zero: number;
}

export function solution(numbers: number[]): SignCount {
return {
positive: numbers.filter(x => x > 0).length,
negative: numbers.filter(x => x < 0).length,
zero: numbers.filter(x => x === 0).length
}
}
16 changes: 16 additions & 0 deletions level-01/1.8-절댓값합/solution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, it, expect } from "vitest";
import { solution } from "./solution";

describe("1.8 절댓값의 합", () => {
it("음수의 절댓값을 더한다", () => {
expect(solution([-3, 4, -1])).toBe(8);
});

it("빈 배열은 0", () => {
expect(solution([])).toBe(0);
});

it("모두 양수", () => {
expect(solution([1, 2, 3])).toBe(6);
});
});
17 changes: 17 additions & 0 deletions level-01/1.8-절댓값합/solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 1.8 절댓값의 합 [프로그래머스 Lv0 / 백준 Bronze IV]
*
* 정수 배열을 받아 각 원소의 절댓값을 모두 더한 값을 반환하세요.
*
* @example
* solution([-3, 4, -1]) // => 8
*/
export function solution(numbers: number[]): number {
let num = 0;
for (let i = 0; i < numbers.length; i++) {
const num2 = Math.abs(numbers[i]);
num += num2;
}

return num
}
Loading