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

Transpose 2 #31820

Open
E0SelmY4V opened this issue Jan 23, 2024 · 1 comment · May be fixed by #31821
Open

Transpose 2 #31820

E0SelmY4V opened this issue Jan 23, 2024 · 1 comment · May be fixed by #31821
Labels
new-challenge Propose a new challenge, a PR will be auto generated

Comments

@E0SelmY4V
Copy link
Contributor

E0SelmY4V commented Jan 23, 2024

Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.

Detailed solution/guide is not required, but please be sure the challenge is solvable.

Info

Basic info of your challenge questions,

difficulty: hard # medium / hard / extreme
title: transpose 2
# tags: array, matrix, transpose # separate by comma
related: 25270

Question

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.

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!

Template

This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation any.

type Matrix = readonly (readonly any[])[];
type Transposed<T extends Matrix> = any;

Test Cases

Provide some test cases for your challenge, you can use some utils from @type-challenges/utils for asserting.

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][]
  ]>>
]
@E0SelmY4V E0SelmY4V added the new-challenge Propose a new challenge, a PR will be auto generated label Jan 23, 2024
@github-actions github-actions bot linked a pull request Jan 23, 2024 that will close this issue
Copy link
Contributor

github-actions bot commented Jan 23, 2024

#31821 - Pull Request updated.

2024-01-26T18:17:47.934Z Preview in Playground

@E0SelmY4V E0SelmY4V changed the title Transposed Matrix Accurate Transpose Jan 24, 2024
@E0SelmY4V E0SelmY4V changed the title Accurate Transpose Transpose 2 Jan 26, 2024
github-actions bot pushed a commit that referenced this issue Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new-challenge Propose a new challenge, a PR will be auto generated
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant