-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00274-extreme-integers-comparator - wip.ts
137 lines (108 loc) · 4.43 KB
/
00274-extreme-integers-comparator - wip.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// WIP - the logic's here, it just needs to be optimized as we're getting excessive type instantiation on nested string to number.
/*
274 - Integers Comparator
-------
by Pig Fang (@g-plane) #extreme #template-literal #math
### Question
Implement a type-level integers comparator. We've provided an enum for indicating the comparison result, like this:
- If `a` is greater than `b`, type should be `Comparison.Greater`.
- If `a` and `b` are equal, type should be `Comparison.Equal`.
- If `a` is lower than `b`, type should be `Comparison.Lower`.
**Note that `a` and `b` can be positive integers or negative integers or zero, even one is positive while another one is negative.**
> View on GitHub: https://tsch.js.org/274
*/
/* _____________ Your Code Here _____________ */
enum Comparison {
Greater,
Equal,
Lower,
}
type AsArray<Number extends number, Array extends any[] = []> =
Array extends { length: Number }
? Array
: AsArray<Number, [...Array, any]>
// todo: what about hitting a negative here? pre-condition that L > R?
type Minus_no<Left extends number, Right extends number> =
AsArray<Left> extends [...infer Minuend, AsArray<Right>]
? Minuend['length']
: never
// type X = Minus_no<1, 555>
// type Minus<Left extends number, Right extends number> =
// AsArray<Right> extends [...infer LeftDiff, ...AsArray<Left>]
// ? LeftDiff extends [] // then Right > Left
// : AsArray<Left> extends [...infer RightDiff, ...AsArray<Right>]
// ? RightDiff['length']
// : never
// type Comparator<A extends number, B extends number> =
// AsArray<A> extends [...infer ADiff, AsArray<B>]
// ? ADiff['length'] extends 0
// ? AsArray<B> extends [...infer BDiff, AsArray<A>]
// ? BDiff['length'] extends 0
// ? Comparison.Lower
// : Comparison.Equal
// : never
// : Comparison.Greater
// : never
/*
to handle neggos
if both pos - done
if one neg - ez?
if both neg - you wanna invert both?
*/
type StringToNumber<T extends string, A extends any[] = []> =
T extends keyof [0, ...A] ? A['length'] : StringToNumber<T, [0, ...A]>
type IsNegative<A extends number> = `${A}` extends `-${string}` ? true : false
type Absolute<A extends number> = `${A}` extends `-${infer B}` ? StringToNumber<B> : A
type IsNaturalLessThanOrEqualTo<A extends number, B extends number> =
AsArray<A> extends [...infer Diff, ...AsArray<B>]
? Diff['length'] extends 0
? true
: false
: true
type IsLessThanOrEqualTo<A extends number, B extends number> =
// AsArray<A> extends [...infer Diff, ...AsArray<B>]
// ? Diff['length'] extends 0
// ? true
// : false
// : true
// type IsAbsoluteLessThanOrEqualTo<A extends number, B extends number>
// = IsNaturalLessThanOrEqualTo<Absolute<A>, Absolute<B>>
// type IsLessThanOrEqualTo<A extends number, B extends number> =
// AsArray<A> extends [...infer Diff, ...AsArray<B>]
// ? Diff['length'] extends 0
// ? true
// : false
// : true
// type Comparator<A extends number, B extends number> =
// IsLessThanOrEqualTo<A, B> extends true
// ? IsLessThanOrEqualTo<B, A> extends true
// ? Comparison.Equal
// : Comparison.Lower
// : Comparison.Greater
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Comparator<5, 5>, Comparison.Equal>>,
Expect<Equal<Comparator<5, 6>, Comparison.Lower>>,
Expect<Equal<Comparator<5, 8>, Comparison.Lower>>,
Expect<Equal<Comparator<5, 0>, Comparison.Greater>>,
Expect<Equal<Comparator<-5, 0>, Comparison.Lower>>,
Expect<Equal<Comparator<0, 0>, Comparison.Equal>>,
Expect<Equal<Comparator<0, -5>, Comparison.Greater>>,
Expect<Equal<Comparator<5, -3>, Comparison.Greater>>,
Expect<Equal<Comparator<5, -7>, Comparison.Greater>>,
Expect<Equal<Comparator<-5, -7>, Comparison.Greater>>,
Expect<Equal<Comparator<-5, -3>, Comparison.Lower>>,
Expect<Equal<Comparator<-25, -30>, Comparison.Greater>>,
Expect<Equal<Comparator<15, -23>, Comparison.Greater>>,
Expect<Equal<Comparator<40, 37>, Comparison.Greater>>,
Expect<Equal<Comparator<-36, 36>, Comparison.Lower>>,
Expect<Equal<Comparator<27, 27>, Comparison.Equal>>,
Expect<Equal<Comparator<-38, -38>, Comparison.Equal>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/274/answer
> View solutions: https://tsch.js.org/274/solutions
> More Challenges: https://tsch.js.org
*/