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

#31820 - Transposed Matrix #31821

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions questions/31820-hard-transpose-2/README.md
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!
7 changes: 7 additions & 0 deletions questions/31820-hard-transpose-2/info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
difficulty: hard
title: transpose 2
related: 25270
author:
github: E0SelmY4V
name: Eosellmay Li

2 changes: 2 additions & 0 deletions questions/31820-hard-transpose-2/template.ts
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;
112 changes: 112 additions & 0 deletions questions/31820-hard-transpose-2/test-cases.ts
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][]
]>>
]