-
-
Save gtkatakura/95312ba5028eacb1eae7c3ffe8d264f8 to your computer and use it in GitHub Desktop.
Median of numers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Reverse<A> = | |
`${A}` extends `${infer AH}${infer AT}` | |
? `${Reverse<AT>}${AH}` : A | |
type Digs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
type DigsNext<I = Digs, R = {}> = | |
I extends [infer Head, infer Next, ...infer Tail] | |
? DigsNext<[Next, ...Tail], R & Record<Head, Next>> | |
: { [K in keyof R]: R[K] } | |
type DigsPrev = { [K in keyof DigsNext as DigsNext[K]]: K } | |
type AddOne<A> = | |
A extends `${infer AH}${infer AT}` | |
? AH extends '9' ? `0${AddOne<AT>}` : `${DigsNext[AH]}${AT}` | |
: `1` | |
type SubOne<A> = | |
A extends `${infer AH}${infer AT}` | |
? AH extends '0' ? `9${SubOne<AT>}` : `${DigsPrev[AH]}${AT}` | |
: never | |
type Add<A, B> = | |
A extends `${infer AH}${infer AT}` ? | |
B extends `${infer BH}${infer BT}` | |
? BH extends '0' ? `${AH}${Add<AT, BT>}` : Add<AddOne<A>, SubOne<B>> | |
: A : B | |
type Mul<A, B, R = '0'> = | |
A extends '0' ? R : | |
B extends '0' ? R : | |
A extends `${infer AH}${infer AT}` | |
? AH extends '0' ? Mul<AT, `0${B}`, R> : Mul<SubOne<A>, B, Add<R, B>> | |
: R | |
type Multiply<A extends string | number | bigint, B extends string | number | bigint> = | |
Reverse<Mul<Reverse<A>, Reverse<B>>> | |
type GreaterThan<T extends number, | |
U extends number, | |
C extends unknown[] = []> = | |
T extends U | |
? false | |
: C['length'] extends T | |
? false | |
: C['length'] extends U | |
? true | |
: GreaterThan<T, U, [...C, 1]>; | |
type ToNumber<S extends string, | |
L extends number[] = []> = | |
`${L['length']}` extends S | |
? L['length'] | |
: ToNumber<S, [...L, 0]>; | |
type Sub<A extends string, | |
B extends string, | |
R extends string = "0"> = | |
B extends R | |
? A | |
: Sub<SubOne<A>, B, AddOne<R>>; | |
const sub: Sub<"5", "2"> = "3" | |
type Division<W extends string, | |
D extends string, | |
Q extends string = '0'> = | |
W extends '0' | |
? Q | |
: Division<Sub<W, D>, D, AddOne<Q>> | |
const div: Division<"3", "2"> = "2" | |
type Len<Arr extends Array<number>> = Arr[0] | |
const d: Len<[1, 2]> | |
// [1,2,3] -> 2 | |
type GreaterThan< | |
T extends number, | |
U extends number, | |
C extends unknown[] = [] | |
> = | |
T extends U | |
? false | |
: C['length'] extends T | |
? false | |
: C['length'] extends U | |
? true | |
: GreaterThan<T, U, [...C, 1]>; | |
const a: GreaterThan<9, 0> // true | |
const a: GreaterThan<0, 9> // false | |
const a: GreaterThan<0, 0> // false | |
// used this issue as idea to make a insert sort https://github.com/type-challenges/type-challenges/issues/856 | |
type Insert< | |
N extends number, | |
A extends Array<any>, | |
> = | |
A extends [infer C, ...infer R] | |
? GreaterThan<N, C> extends false | |
? [N, ...A] | |
: [C, ...Insert<N, R>] | |
: [N] | |
type Sort< | |
V extends Array<number>, | |
A extends Array<any> = [] | |
> = | |
V extends [infer C, ...infer R] | |
? Sort<R, Insert<C, A>> | |
: A | |
let t: Sort<[2, 2, 1, 5, 6]>; // [1, 2, 2, 5, 6] | |
let t: Sort<[5, 20, 10000000002, 1, 25]>; // [1, 5, 20, 25, 10000000002] | |
let t: Sort<[5, 20, 121, 1, 25, 26, 512]>; // [1, 5, 20, 25, 26, 121, 512] | |
type Average | |
<Arr extends Array<any>, OriginalLen extends string = `${Arr["length"]}`,Acc extends string = "0"> = | |
Arr extends [infer E, ...infer R] | |
? Average<R, OriginalLen, Add<E, Acc>> | |
: Division<Acc, OriginalLen> | |
const md: Average<["1","2","3"]> // 2 | |
const an: Average<["4","2","3"]> // 3 | |
const me: Average<["1","2","0", "1"]> // 1 | |
type ToStr<V extends any> = `${V}` | |
type Median | |
<Arr extends Array<any>> = | |
Division<`${Arr["length"]}`, '2'> extends never | |
? Sort<Arr>[Division<AddOne<`${Arr["length"]}`>, "2">] | |
: Division< | |
Add< | |
ToStr<Sort<Arr>[Division<`${Arr["length"]}`, '2'>]>, | |
ToStr<Sort<Arr>[Division<AddOne<`${Arr["length"]}`>, "2">]> | |
>, | |
"2"> | |
const medianImpar: Median<[1, 2, 4, 3, 2]> // ímpar ~> 3 | |
const medianPar: Median<[2, 3, 2, 1]> // par ~> 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment