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

274 - Integers Comparator - any numbers #11540

Open
teamchong opened this issue Jun 13, 2022 · 0 comments
Open

274 - Integers Comparator - any numbers #11540

teamchong opened this issue Jun 13, 2022 · 0 comments
Labels
274 answer Share answers/solutions to a question en in English

Comments

@teamchong
Copy link

teamchong commented Jun 13, 2022

enum Comparison {
  Greater,
  Equal,
  Lower,
}

type Comparator<X extends number, Y extends number> = {'<': Comparison.Lower, '=': Comparison.Equal, '>': Comparison.Greater}[CompareNumbers<`${X}`, `${Y}`>]

// Type to compare two numeric types X and Y and determine their relational order as '<', '=', or '>'.
type CompareNumbers<X extends string, Y extends string>
  = `${X}${Y}` extends `-${infer A}-${infer B}` ? CompareNumbers<B, A> // Handles comparison when both numbers are negative, invert the comparison order to compare their positive counterparts
  : X extends `-${any}` ? '<' // If X is negative and Y is not Y is greater
  : Y extends `-${any}` ? '>' // If Y is negative and X is not X is greater
  : `${X} ${Y}` extends `${infer A}e+${infer E} ${infer B}e+${infer F}` ? CompareInteger<E, F> extends '=' ? CompareNumbers<A, B> : CompareInteger<E, F>
  : X extends `${any}e+${any}` ? '>'
  : Y extends `${any}e+${any}` ? '<'
  : `${X} ${Y}` extends `${infer A}e-${infer E} ${infer B}e-${infer F}` ? CompareInteger<F, E> extends '=' ? CompareNumbers<A, B> : CompareInteger<F, E>
  : X extends `${any}e-${any}` ? '<'
  : Y extends `${any}e-${any}` ? '>'
  : `${X} ${Y}` extends `${infer A}.${infer C} ${infer B}.${infer D}` ? CompareInteger<A, B> extends '=' ? CompareFractional<C, D> : CompareInteger<A, B>
  : X extends `${infer A}.${any}` ? CompareInteger<A, Y> extends '=' ? '>' : CompareInteger<A, Y>
  : Y extends `${infer B}.${any}` ? CompareInteger<X, B> extends '=' ? '<' : CompareInteger<X, B>
  : CompareInteger<X, Y>

// Recursive type to compare integer strings and determine their order.
type CompareInteger<X extends string, Y extends string, PreviousComparison extends '<' | '=' | '>' = '='>
  = X extends Y ? PreviousComparison // If both strings are equal, return the previous comparison result
  : `${X} ${Y}` extends `${infer A}${infer C} ${infer B}${infer D}`
    ? CompareInteger<C, D,
        PreviousComparison extends '<' | '>' ? PreviousComparison // If difference has been found, Carry forward the first difference
        : A extends B ? '=' // If current digits are the same, skip to next
        : '0123456789' extends `${any}${A}${any}${B}${any}` ? '<' : '>'> // Compare digits lexicographically
  : X extends '' ? '<' : '>' // longer is greater

type CompareFractional<X extends string, Y extends string>
  = `${X} ${Y}` extends `${infer A}${infer C} ${infer B}${infer D}`
    ? A extends B ? CompareFractional<C, D> // If current digits are the same, skip to next
    : '0123456789' extends `${any}${A}${any}${B}${any}` ? '<' : '>' // Compare digits lexicographically
  : X extends '' ? '<' : '>' // longer is greater

Playground

@teamchong teamchong added answer Share answers/solutions to a question en in English labels Jun 13, 2022
@github-actions github-actions bot added the 274 label Jun 13, 2022
@teamchong teamchong changed the title 274 - Integers Comparator 274 - Integers Comparator - any numbers Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
274 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

1 participant