-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(question): add #31820 - accurate transpose
- Loading branch information
Showing
4 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Transposition is a math concept. | ||
We can use a 2d tuples to describe a matrix in TS. | ||
But tuples in TS aren't always squares, which makes the transposition of 2d tuples become more difficult. | ||
|
||
```ts | ||
type SomeMatrix = [ | ||
[1, 2, 3], | ||
[4, 5], | ||
[6] | ||
]; | ||
type TransposedSomeMatrix = [ | ||
[1, 4, 6], | ||
[2, 5], | ||
[3] | ||
]; | ||
``` | ||
|
||
It's easy to understand, so the mission of making a type which can transpose given 2d tuple as accurate as possible is now assigned to you! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
difficulty: hard | ||
title: accurate transpose | ||
related: 25270 | ||
author: | ||
github: E0SelmY4V | ||
name: Eosellmay Li | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
type Matrix = readonly (readonly any[])[]; | ||
type Transposed<T extends Matrix> = any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { Equal, Expect } from '@type-challenges/utils' | ||
import { ExpectFalse, NotEqual } from '@type-challenges/utils' | ||
|
||
type cases = [ | ||
Expect<Equal<Transposed<[ | ||
[1, 2, 3], | ||
[4, 5, 6], | ||
[7, 8, 9] | ||
]>, [ | ||
[1, 4, 7], | ||
[2, 5, 8], | ||
[3, 6, 9] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
['a', 'b', 'c', 'd'], | ||
['e', 'f', 'g', 'h'], | ||
['i', 'j', 'k', 'l'] | ||
]>, [ | ||
['a', 'e', 'i'], | ||
['b', 'f', 'j'], | ||
['c', 'g', 'k'], | ||
['d', 'h', 'l'] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, 2, 3], | ||
[4, 5] | ||
]>, [ | ||
[1, 4], | ||
[2, 5], | ||
[3] | ||
]>>, | ||
Expect<Equal<Transposed<number[][]>, number[][]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, 2], | ||
[3], | ||
[4, 5] | ||
]>, [ | ||
[1, 3, 4], | ||
[2, undefined, 5] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, 2], | ||
[3], | ||
...[4][], | ||
]>, [ | ||
[1, 3, ...4[]], | ||
[2] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, 2], | ||
[3], | ||
...[4, 5, 6][], | ||
]>, [ | ||
[1, 3, ...4[]], | ||
[2, undefined, ...5[]], | ||
[undefined, undefined, ...6[]] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, 2, 3], | ||
[4], | ||
...[5][], | ||
[6, 7] | ||
]>, [ | ||
[1, 4, ...5[], 6], | ||
[2, undefined, ...undefined[], 7], | ||
[3] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, ...2[]], | ||
[3, 4, ....5[]] | ||
]>, [ | ||
[1, 3], | ||
[2 | undefined, 4], | ||
...[2 | undefined, 5 | undefined][] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, ...2[], 3], | ||
[4, 5, ....6[], 7, 8], | ||
[...9[], 0] | ||
]>, [ | ||
[1, 4, 9 | 0], | ||
[2 | 3, 5, 9 | 0 | undefined], | ||
[2 | 3 | undefined , 6 | 7, 9 | 0 | undefined], | ||
[2 | 3 | undefined , 6 | 7 | 8, 9 | 0 | undefined], | ||
...[2 | 3 | undefined , 6 | 7 | 8 | undefined, 9 | 0 | undefined][] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1, 2], | ||
...[3, ...4[]][] | ||
]>, [ | ||
[1, ...3[]], | ||
[2, ...4[]], | ||
...[undefined, ...4[]][] | ||
]>>, | ||
Expect<Equal<Transposed< | ||
[1, 2, 3][] | ||
>, [ | ||
1[], | ||
2[], | ||
3[] | ||
]>>, | ||
Expect<Equal<Transposed<[ | ||
[1], | ||
...[2, ...3[], 4, 5][], | ||
[6, 7, ...8[], 9] | ||
]>, [ | ||
[1, ...2[], 6], | ||
[undefined, ...(3 | 4)[], 7], | ||
[undefined, ...(3 | 4 | 5)[], 8 | 9], | ||
...[undefined, ...(3 | 4 | 5 | undefined)[], 8 | 9 | undefined][] | ||
]>> | ||
] |