You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Iterate over T // -> If current element CURR > TARGET and TARGET - CURR exists in the remainder of T, return true.// -> Else, recursively call TwoSum over remainder of T.// -> If iteration is completed, return false.typeTwoSum<Textendsnumber[],TARGETextendsnumber,CURRextendsnumber=T[0],NEXTextendsnumber[]=Shift<T>,RESULTextendsboolean=Textends[] ? false
: LessThan<TARGET,CURR>extendstrue ? TwoSum<NEXT,TARGET>
: Subtract<TARGET,CURR>extendsNEXT[number] ? true
: TwoSum<NEXT,TARGET>>=RESULT;
/** Helpers */// new Array(N).fill(T)typeRepeat<Nextendsnumber,Textendsany=null,MextendsT[]=[]>=M["length"]extendsN ? M : Repeat<N,T,[T, ...M]>;// If A >= B, return A - B. Else, return never.typeSubtract<Aextendsnumber,Bextendsnumber>=Repeat<A>extends[...Repeat<B>, ...infer Rest]
? Rest["length"]
: never;typeLessThan<Textendsnumber,Uextendsnumber>=Equal<T,U>extendstrue ? false
: Subtract<T,U>extendsnever ? true : false;typeShift<Textendsany[],Nextendsnumber=1>=Nextends0 ? T
: Textends[infer _, ...infer Rest]
? Shift<Rest,Subtract<N,1>>
: [];
typeTwoSumNewTests=[// The following incorrect solution passes all other test cases:// ... ? Subtract<U, E> extends never// ? false ...Expect<Equal<TwoSum<[3,2,0],2>,true>>];typeSubtractTests=[Expect<Equal<Subtract<0,0>,0>>,Expect<Equal<Subtract<1,0>,1>>,Expect<Equal<Subtract<2,1>,1>>,Expect<Equal<Subtract<1,2>,never>>,];typeRepeatTests=[Expect<Equal<Repeat<0>,[]>>,Expect<Equal<Repeat<3>,[null,null,null]>>,Expect<Equal<Repeat<3,"1">,["1","1","1"]>>,];
The text was updated successfully, but these errors were encountered:
- Currently, all test cases are written s.t. all input elements are leq to the target number.
- If TwoSum is written to terminate and return false when an input element is greater than the target number, it will pass all existing test cases, resulting in a Type II error.
- Example:
```
T extends [infer E extends number, ...infer Rest extends number[]]
? Subtract<U, E> extends never
? false
: (...processes Rest correctly...)
```
- The proposed addition tests against a non-empty tuple with a head element that is greater than the target number.
See: 8804 - Two Sum (with explanation, new test case) type-challenges#21260
MajorLift
added a commit
to MajorLift/type-challenges
that referenced
this issue
Jan 3, 2023
- Currently, all test cases are written s.t. all input elements are leq to the target number.
- If TwoSum is written to terminate and return false when an input element is greater than the target number, it will pass all existing test cases, resulting in a Type II error.
- Example:
```
T extends [infer E extends number, ...infer Rest extends number[]]
? Subtract<U, E> extends never
? false
: (...processes Rest correctly...)
```
- The proposed addition tests against a non-empty tuple with a head element that is greater than the target number.
See: 8804 - Two Sum (with explanation, new test case) type-challenges#21260
The text was updated successfully, but these errors were encountered: