Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8804 - Two Sum (with explanation, new test case) #21260

Open
MajorLift opened this issue Dec 29, 2022 · 0 comments
Open

8804 - Two Sum (with explanation, new test case) #21260

MajorLift opened this issue Dec 29, 2022 · 0 comments
Labels
8804 answer Share answers/solutions to a question en in English

Comments

@MajorLift
Copy link
Contributor

MajorLift commented Dec 29, 2022

// 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.
type TwoSum<
  T extends number[], 
  TARGET extends number,
  CURR extends number = T[0],
  NEXT extends number[] = Shift<T>,
  RESULT extends boolean = T extends [] ? false
    : LessThan<TARGET, CURR> extends true ? TwoSum<NEXT, TARGET>
    : Subtract<TARGET, CURR> extends NEXT[number] ? true
      : TwoSum<NEXT, TARGET>
> = RESULT;
/** Helpers */
// new Array(N).fill(T)
type Repeat<N extends number, T extends any = null, M extends T[] = []> = 
  M["length"] extends N ? M : Repeat<N, T, [T, ...M]>;

// If A >= B, return A - B. Else, return never.
type Subtract<A extends number, B extends number> = 
  Repeat<A> extends [...Repeat<B>, ...infer Rest] 
    ? Rest["length"]
    : never;

type LessThan<T extends number, U extends number> = 
  Equal<T, U> extends true ? false
    : Subtract<T, U> extends never ? true : false;

type Shift<T extends any[], N extends number = 1> = 
  N extends 0 ? T 
    : T extends [infer _, ...infer Rest]
      ? Shift<Rest, Subtract<N, 1>>
      : [];
type TwoSumNewTests = [
  // The following incorrect solution passes all other test cases:
  // ... ? Subtract<U, E> extends never
  //       ? false ...
  Expect<Equal<TwoSum<[3, 2, 0], 2>, true>>
];

type SubtractTests = [
  Expect<Equal<Subtract<0, 0>, 0>>,
  Expect<Equal<Subtract<1, 0>, 1>>,
  Expect<Equal<Subtract<2, 1>, 1>>,
  Expect<Equal<Subtract<1, 2>, never>>,
];

type RepeatTests = [
  Expect<Equal<Repeat<0>, []>>,
  Expect<Equal<Repeat<3>, [null, null, null]>>,
  Expect<Equal<Repeat<3, "1">, ["1", "1", "1"]>>,  
];
@MajorLift MajorLift added answer Share answers/solutions to a question en in English labels Dec 29, 2022
@github-actions github-actions bot added the 8804 label Dec 29, 2022
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8804 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant