首页 > 其他分享 >[Typescript] 124. Binary to Decimal

[Typescript] 124. Binary to Decimal

时间:2022-12-02 21:57:16浏览次数:49  
标签:Binary NumberToArray Typescript _____________ Decimal extends Expect type Binary

Implement BinaryToDecimal<S> which takes an exact string type S consisting 0 and 1 and returns an exact number type corresponding with S when S is regarded as a binary. You can assume that the length of S is equal to or less than 8 and S is not empty.

type Res1 = BinaryToDecimal<'10'>; // expected to be 2
type Res2 = BinaryToDecimal<'0011'>; // expected to be 3

 

/* _____________ Your Code Here _____________ */
type NumberToArray<T extends number, R extends 1[] = []> = R['length'] extends T ? R : NumberToArray<T, [...R, 1]>;

type GetTwice<T extends unknown[]> = [...T, ...T];

type BinaryToDecimal<S extends string, Result extends unknown[] = []> = S extends `${infer First extends number}${infer RT}`
  ? BinaryToDecimal<RT, [...GetTwice<Result>, ...NumberToArray<First>]>
  : Result['length'];


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<BinaryToDecimal<'10'>, 2>>,
  Expect<Equal<BinaryToDecimal<'0011'>, 3>>,
  Expect<Equal<BinaryToDecimal<'00000000'>, 0>>,
  Expect<Equal<BinaryToDecimal<'11111111'>, 255>>,
  Expect<Equal<BinaryToDecimal<'10101010'>, 170>>,
]

 

标签:Binary,NumberToArray,Typescript,_____________,Decimal,extends,Expect,type,Binary
From: https://www.cnblogs.com/Answer1215/p/16945728.html

相关文章

  • BigDecimal类型的值比较大小
    使用compareTo方法:eg1:intresult=bigdemical1.compareTo(bigdemical2)result=-1,表示bigdemical1小于bigdemical2;result=0,表示bigdemical1等于bigdemical2;result=......
  • 124. Binary Tree Maximum Path Sum(难)
    Givenabinarytree,findthemaximumpathsum.Forthisproblem,apathisdefinedasanysequenceofnodesfromsomestartingnodetoanynodeinthetreeal......
  • 166. Fraction to Recurring Decimal
    Giventwointegersrepresentingthenumeratoranddenominatorofafraction,returnthefractioninstringformat.Ifthefractionalpartisrepeating,enclose......
  • 98. Validate Binary Search Tree
    Givenabinarytree,determineifitisavalidbinarysearchtree(BST).AssumeaBSTisdefinedasfollows:Theleftsubtreeofanodecontainsonlynodeswit......
  • Remove Node in Binary Search Tree
    GivenarootofBinarySearchTreewithuniquevalueforeachnode.Removethenodewithgivenvalue.Ifthereisnosuchanodewithgivenvalueinthebinary......
  • lintcode:Binary Tree Serialization
    Designanalgorithmandwritecodetoserializeanddeserializeabinarytree.Writingthetreetoafileiscalled‘serialization’andreadingbackfromthe......
  • BigDecimal
    Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数,但在实际应用中,可能需要对更大或者更小的......
  • TypeScript
    报错函数实现重复法一:export{};//第一行增加这个语句是为了使文件里的变量不污染全局法二:在项目根目录运行:tsc--init之后根目录会生成一个json文件,文件名为:tscon......
  • jaava之BigDecimal
    BigDecimal常见异常除法的时候出现异常java.lang.ArithmeticException:Non-terminatingdecimalexpansion;noexactrepresentabledecimalresult原因分析:......
  • [Typescript 4.9] Satisfies operator
    typeRGB=[number,number,number]constpalette:Record<'red'|'blue'|'green',string|RGB>={red:[255,0,0],green:"#00ff00",blue:[0,0,255......