Skip to content

Instantly share code, notes, and snippets.

@Grubba27
Last active May 31, 2022 04:10
Show Gist options
  • Save Grubba27/4d05d6533a3dc2bcac113aa237eca939 to your computer and use it in GitHub Desktop.
Save Grubba27/4d05d6533a3dc2bcac113aa237eca939 to your computer and use it in GitHub Desktop.
basico of Statistics
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
type Mode
<List extends Array<any>,
CurrentNumber extends any = List[0],
CurrentArray extends Array<any> = [],
Acc extends Array<Array<any>> = [[]]>
= List extends [infer E, ...infer R]
? E extends CurrentNumber
? Mode<R, CurrentNumber, [CurrentNumber, ...CurrentArray], Acc>
: GreaterThan<CurrentArray["length"], Acc[0]["length"]> extends true
? Mode<[...R, 0], E, [], [ CurrentArray, ...Acc] >
: Mode<[...R, 0], E, [], [ ...Acc, CurrentArray] >
: Acc[0][0]
const mod2: Mode<[1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3]> // 2
const mod1: Mode<[1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3]> // 1
const mod3: Mode<[1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]> // 3
const mod4: Mode<[1,1,1,1,1,4,3]> // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment