这是indexloc提供的服务,不要输入任何密码
Skip to content

4425 - Greater Than (🎥 Video Explanation and Solution) #24326

@dimitropoulos

Description

@dimitropoulos

4425 - Greater Than

Imagine being able to compute values all in a type system! What a time to be alive! Can you write a GreaterThan implementation that also handles negative numbers?... hmmm..

🎥 Video Explanation

Release Date: 2023-03-07 19:00 UTC

Greater Than

🔢 Code

// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'

type A1 = GreaterThan<1, 0>;
type B1 = true;
type C1 = Expect<Equal<A1, B1>>;

type A2 = GreaterThan<5, 4>;
type B2 = true;
type C2 = Expect<Equal<A2, B2>>;

type A3 = GreaterThan<4, 5>;
type B3 = false;
type C3 = Expect<Equal<A3, B3>>;

type A4 = GreaterThan<0, 0>;
type B4 = false;
type C4 = Expect<Equal<A4, B4>>;

type A5 = GreaterThan<20, 20>;
type B5 = false;
type C5 = Expect<Equal<A5, B5>>;

type A6 = GreaterThan<10, 100>;
type B6 = false;
type C6 = Expect<Equal<A6, B6>>;

type A7 = GreaterThan<111, 11>;
type B7 = true;
type C7 = Expect<Equal<A7, B7>>;

// ============= Your Code Here =============
type GreaterThan<
  A extends number,
  B extends number,
  Count extends 1[] = []
> =
  Count['length'] extends A
  ? false
  : Count['length'] extends B
    ? true
    : GreaterThan0<A, B, [...Count, 1]>


// ============== Alternatives ==============
// github @xinchaobeta
type Digit = '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9';
type StringLengthCompare<
  A extends string,
  B extends string
> =
  A extends `${Digit}${infer AR}`
  ? B extends `${Digit}${infer BR}`
    ? StringLengthCompare<AR, BR>
    : 1
  : B extends `${Digit}${string}`
    ? -1
    : 0;

type DigitCompare<A extends Digit, B extends Digit> =
  A extends B
  ? 0
  : A extends "0"
    ? -1
    : A extends "1"
      ? B extends "0"
      ? 1
      : -1
    : A extends "2"
      ? B extends "0"|"1"
        ? 1
        : -1
      : A extends "3"
        ? B extends "0"|"1"|"2"
          ? 1
          : -1
        : A extends "4"
          ? B extends "0"|"1"|"2"|"3"
            ? 1
            : -1
          : A extends "5"
            ? B extends "9"|"8"|"7"|"6"
              ? -1
              : 1
            : A extends "6"
              ? B extends "9"|"8"|"7"
                ? -1
                : 1
              : A extends "7"
                ? B extends "9"|"8"
                  ? -1
                  : 1
                : A extends "8"
                  ? B extends "9"
                    ? -1
                    : 1
                  : A extends "9"
                    ? 1
                    : 1;

type SameLengthCompare<
  A extends string,
  B extends string,
> = 
  A extends `${infer D1}${infer R1}`
  ? B extends `${infer D2}${infer R2}`
    ? DigitCompare<D1 & Digit, D2 & Digit> extends infer R
      ? R extends 0
        ? SameLengthCompare<R1, R2>
          : R
        : never
      : 0
    : 0;

type GreaterThanHelper<
  A extends string,
  B extends string,
  StrComp = StringLengthCompare<A, B>,
  SameComp = SameLengthCompare<A, B>
> = StrComp extends 0 ? SameComp : StrComp;

type GreaterThan<
  A extends number,
  B extends number
> =
  GreaterThanHelper<`${A}`, `${B}`> extends 1
  ? true
  : false;

➕ More Solutions

For more video solutions to other challenges: see the umbrella list! #21338

Metadata

Metadata

Assignees

No one assigned

    Labels

    4425answerShare answers/solutions to a questionenin English

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions