Skip to content

Commit d9b8fec

Browse files
committed
add 2 ts medium challenges
1 parent ae64c77 commit d9b8fec

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
529 - Absolute
3+
-------
4+
by Andrey Krasovsky (@bre30kra69cs) #medium #math #template-literal
5+
6+
### Question
7+
8+
Implement the `Absolute` type. A type that take string, number or bigint. The output should be a positive number string
9+
10+
For example
11+
12+
```ts
13+
type Test = -100;
14+
type Result = Absolute<Test>; // expected to be "100"
15+
```
16+
17+
> View on GitHub: https://tsch.js.org/529
18+
*/
19+
20+
/* _____________ Your Code Here _____________ */
21+
// my try
22+
type Absolute<T extends number | string | bigint> = T extends string
23+
? T extends `${infer H}${infer R}`
24+
? H extends "-"
25+
? `${R}`
26+
: `${T}`
27+
: `${T}`
28+
: Absolute<`${T}`>;
29+
30+
// shortest offical answer
31+
type AbsoluteOfficial<T extends number | string | bigint> =
32+
`${T}` extends `-${infer R}` ? `${R}` : `${T}`;
33+
34+
type Test = -5;
35+
type Result = Absolute<Test>; // expected to be "100"
36+
/* _____________ Test Cases _____________ */
37+
import type { Equal, Expect } from "@type-challenges/utils";
38+
39+
type cases = [
40+
Expect<Equal<Absolute<0>, "0">>,
41+
Expect<Equal<Absolute<-0>, "0">>,
42+
Expect<Equal<Absolute<10>, "10">>,
43+
Expect<Equal<Absolute<-5>, "5">>,
44+
Expect<Equal<Absolute<"0">, "0">>,
45+
Expect<Equal<Absolute<"-0">, "0">>,
46+
Expect<Equal<Absolute<"10">, "10">>,
47+
Expect<Equal<Absolute<"-5">, "5">>,
48+
Expect<Equal<Absolute<-1_000_000n>, "1000000">>,
49+
Expect<Equal<Absolute<9_999n>, "9999">>
50+
];
51+
52+
/* _____________ Further Steps _____________ */
53+
/*
54+
> Share your solutions: https://tsch.js.org/529/answer
55+
> View solutions: https://tsch.js.org/529/solutions
56+
> More Challenges: https://tsch.js.org
57+
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
531 - String to Union
3+
-------
4+
by Andrey Krasovsky (@bre30kra69cs) #medium #union #string
5+
6+
### Question
7+
8+
Implement the String to Union type. Type take string argument. The output should be a union of input letters
9+
10+
For example
11+
12+
```ts
13+
type Test = '123';
14+
type Result = StringToUnion<Test>; // expected to be "1" | "2" | "3"
15+
```
16+
17+
> View on GitHub: https://tsch.js.org/531
18+
*/
19+
20+
/* _____________ Your Code Here _____________ */
21+
22+
type StringToUnion<T extends string> = T extends ""
23+
? never
24+
: T extends `${infer R}${infer K}`
25+
? R | StringToUnion<K>
26+
: never;
27+
28+
type Test = "123";
29+
type Result = StringToUnion<Test>; // expected to be "1" | "2" | "3"
30+
31+
/* _____________ Test Cases _____________ */
32+
import type { Equal, Expect } from "@type-challenges/utils";
33+
34+
type cases = [
35+
Expect<Equal<StringToUnion<"">, never>>,
36+
Expect<Equal<StringToUnion<"t">, "t">>,
37+
Expect<Equal<StringToUnion<"hello">, "h" | "e" | "l" | "l" | "o">>,
38+
Expect<
39+
Equal<
40+
StringToUnion<"coronavirus">,
41+
"c" | "o" | "r" | "o" | "n" | "a" | "v" | "i" | "r" | "u" | "s"
42+
>
43+
>
44+
];
45+
46+
/* _____________ Further Steps _____________ */
47+
/*
48+
> Share your solutions: https://tsch.js.org/531/answer
49+
> View solutions: https://tsch.js.org/531/solutions
50+
> More Challenges: https://tsch.js.org
51+
*/

0 commit comments

Comments
 (0)