알고리즘

알고리즘/Python

백준 16236 - 아기 상어

출처 : 백준, https://www.acmicpc.net/problem/16236 16236번: 아기 상어 N×N 크기의 공간에 물고기 M마리와 아기 상어 1마리가 있다. 공간은 1×1 크기의 정사각형 칸으로 나누어져 있다. 한 칸에는 물고기가 최대 1마리 존재한다. 아기 상어와 물고기는 모두 크기를 가 www.acmicpc.net 더보기 먼저, 생각해야 할 부분 1. 먹을 수 있는 물고기의 위치와 거기까지의 거리를 어떻게 찾을지 생각해야한다. 2. 같은 거리의 물고기 중 위 왼쪽에 위치한 물고기를 찾아야한다. 풀이 import sys from collections import deque KEY_SIZE = 'size' KEY_EAT_FISH = 'eatFish' KEY_POINT = 'point' d..

알고리즘/Type Challenges

8 - Readonly 2

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 8 - Readonly 2 `T`에서 `K` 프로퍼티만 읽기 전용으로 설정해 새로운 오브젝트 타입을 만드는 제네릭 `MyReadonly2`를 구현하세요. `K`가 주어지지 않으면 단순히 `Readonly`처럼 모든 프로퍼티를 읽기 전용으로 설정해야 합니다. interface Todo { title: string description: string completed: boolean } const todo: MyReadonly2 = { title: "Hey", description: "foobar", completed: false, } to..

알고리즘/Type Challenges

3 - Omit

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3 - Omit `T`에서 `K` 프로퍼티만 제거해 새로운 오브젝트 타입을 만드는 내장 제네릭 `Omit`를 이를 사용하지 않고 구현하세요. interface Todo { title: string description: string completed: boolean } type TodoPreview = MyOmit const todo: TodoPreview = { completed: false, } 풀이 type MyOmit = { [P in keyof T as P extends K ? never : P]: T[P] } 대부분은 Pick..

알고리즘/Type Challenges

2 - Get Return Type

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 2 - Get Return Type 내장 제네릭 `ReturnType`을 이를 사용하지 않고 구현하세요. const fn = (v: boolean) => { if (v) return 1 else return 2 } type a = MyReturnType // should be "1 | 2" 풀이 type MyReturnType = T extends (...args: any) => infer ReturnType ? ReturnType : never args는 :을 통해서 파라미터 타입을 지정해주는 반면에 리턴 타입은 바로 infer로 가져..

알고리즘/Type Challenges

898 - Includes

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 898 - Includes JavaScript의 `Array.includes` 함수를 타입 시스템에서 구현하세요. 타입은 두 인수를 받고, `true` 또는 `false`를 반환해야 합니다. type isPillarMen = Includes // expected to be `false` 풀이 type IsEqual = (() => T extends X ? 1 : 2) extends (() => T extends Y ? 1 : 2) ? true : false type Includes = T extends [infer First, ...in..

알고리즘/Type Challenges

3312 - Parameters

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3312 - Parameters 내장 제네릭 ``Parameters``를 이를 사용하지 않고 구현하세요. const foo = (arg1: string, arg2: number): void => {} type FunctionParamsType = MyParameters // [arg1: string, arg2: number] 풀이 type MyParameters any> = T extends (...args: infer Params) => any ? Params : never

알고리즘/Type Challenges

3060 - Unshift

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3060 - Unshift `Array.unshift`의 타입 버전을 구현하세요. type Result = Unshift // [0, 1, 2,] 풀이 type Unshift = T extends [...infer Rest] ? [U, ...Rest] : never

알고리즘/Type Challenges

3057 - Push

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 3057 - Push `Array.push`의 제네릭 버전을 구현하세요. type Result = Push // [1, 2, '3'] 풀이 type Push = T extends [...infer REST] ? [...REST, U] : never infer를 사용해서 ...를 사용해서 배열 내부를 그대로 가져와서 연결하면 된다.

알고리즘/Type Challenges

533 - Concat

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 533 - Concat JavaScript의 `Array.concat` 함수를 타입 시스템에서 구현하세요. 타입은 두 인수를 받고, 인수를 왼쪽부터 concat한 새로운 배열을 반환해야 합니다. type Result = Concat // expected to be [1, 2] 풀이 type Concat = T extends readonly [...infer A] ? U extends readonly [...infer B] ? [...A, ...B] : never : never; extends와 infer를 통해서 T와 U 내부의 타입들을..

알고리즘/Type Challenges

268 - If

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 268 - If 조건 `C`, 참일 때 반환하는 타입 `T`, 거짓일 때 반환하는 타입 `F`를 받는 타입 If를 구현하세요. `C`는 `true` 또는 `false`이고, `T`와 `F`는 아무 타입입니다. type A = If // expected to be 'a' type B = If // expected to be 'b' 풀이 type If = C extends true ? T : F; extends를 이용한 조건문 연습 문제인 듯 하다.

알고리즘/Type Challenges

189 - Awaited

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 189 - Awaited Promise와 같은 타입에 감싸인 타입이 있을 때, 안에 감싸인 타입이 무엇인지 어떻게 알 수 있을까요? type ExampleType = Promise type Result = MyAwaited // string 풀이 type Thenable = { then: (_: (_:T) => any) => any } type MyAwaited = T extends Thenable ? U extends Thenable ? MyAwaited : U : never type MyAwaited = T extends { then..

알고리즘/Type Challenges

43 - Exclude

출처: Type Challenges, https://github.com/type-challenges/type-challenges/blob/main/README.ko.md 43 - Exclude `T`에서 `U`에 할당할 수 있는 타입을 제외하는 내장 제네릭 `Exclude`를 이를 사용하지 않고 구현하세요. type Result = MyExclude // 'b' | 'c' 풀이 type MyExclude = T extends U ? never : T; 타입스크립트의 타입시스템은 유니온 타입을 분배시켜 처리한다. 예를 들어 `'a' | 'b' | 'c' extends 'a'`라면 이는 `'a' extends 'a' | 'b' extends 'a' | 'c' extends 'a'`로 처리된다. 따라서 `My..

제주도랏맨
'알고리즘' 카테고리의 글 목록