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 #25523

Open
E-uler opened this issue Mar 23, 2023 · 0 comments
Open

274 - Integers Comparator #25523

E-uler opened this issue Mar 23, 2023 · 0 comments
Labels
274 answer Share answers/solutions to a question en in English

Comments

@E-uler
Copy link

E-uler commented Mar 23, 2023

type Digit = ToArray<`0123456789`>;
type ToArray<T extends string | number | bigint> = `${T}` extends `${infer F}${infer R}` ? [F, ...ToArray<R>] : [];
type Length<T extends number | bigint | string> = ToArray<T>["length"];
/**最高位 */
type High<T extends string | number> = `${T}` extends `${infer F}${any}` ? F : ``;
/**更低位 */
type DigitLowerRest<T extends number | string> = `${T}` extends `${any}${infer R}` ? R : ``;
/**绝对值 */
type AbsString<T extends number | string | bigint> = `${T}` extends `-${infer N}` ? N : `${T}`;
/**是否负数 */
type IsNegative<T extends number | string | bigint> = `${T}` extends `-${any}` ? true : false;
/**比较单个位 */
type DigitComparator<T1 extends number | bigint | string, T2 extends number | bigint | string, IsNegative extends boolean = false, U extends string[] = Digit> =
  (T1 extends T2 ?
    Comparison.Equal :
    (AbsString<T1> extends U[0] ?
      (IsNegative extends false ? Comparison.Lower : Comparison.Greater) :    //负值反转
      (AbsString<T2> extends U[0] ?
        (IsNegative extends false ? Comparison.Greater : Comparison.Lower) :  //负值反转
        (U extends [any, ...infer R extends string[]] ? DigitComparator<T1, T2, IsNegative, R> : false)
      ))
  );

enum Comparison {
  Greater,
  Equal,
  Lower,
}
type Comparator<A extends number | string, B extends number | string, _AIsNegative extends boolean = IsNegative<A>, _BIsNegative extends boolean = IsNegative<B>> =
  [_AIsNegative & _BIsNegative] extends [never] ?
  //一正一负
  ([0] extends [A & B] ? Comparison.Equal :   //-0==0
    _AIsNegative extends true ? Comparison.Lower : Comparison.Greater)   //-X<Y
  :
  //正负相同
  (Length<AbsString<A>> extends Length<AbsString<B>> ?
    //位数相同,从高位开始比较
    (`${A}${B}` extends `` ? Comparison.Equal :             //直到全部位都相同 return 相同
      (High<A> extends High<B> ?
        Comparator<DigitLowerRest<A>, DigitLowerRest<B>, _AIsNegative, _BIsNegative> :   //当前位相同,比较更低位
        DigitComparator<High<A>, High<B>, _AIsNegative>)    //当前位不同,retrun
    )
    :
    //位数不同,比较位数大的
    Comparator<Length<AbsString<A>>, Length<AbsString<B>>, _AIsNegative, _BIsNegative>);
@E-uler E-uler added answer Share answers/solutions to a question en in English labels Mar 23, 2023
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