Implement chunks matrix transposed method

This commit is contained in:
Daniel Sanchez Quiros 2024-03-06 16:56:23 +01:00
parent 02ed35596b
commit 9bb5a6270f
2 changed files with 10 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from dataclasses import dataclass
from itertools import chain
from typing import List, Generator
from typing import List, Generator, Self
from eth2spec.eip7594.mainnet import Bytes32
@ -24,7 +24,10 @@ class Row(List[Bytes32]):
class ChunksMatrix(List[Row]):
@property
def columns(self) -> Generator[List[Chunk], None, None]:
yield from map(list, zip(*self))
yield from map(Row, zip(*self))
def transposed(self) -> Self:
return ChunksMatrix(self.columns)

View File

@ -10,3 +10,8 @@ class TestCommon(TestCase):
expected = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
for c1, c2 in zip(expected, matrix.columns):
self.assertEqual(c1, c2)
def test_chunks_matrix_transposed(self):
matrix = ChunksMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
expected = ChunksMatrix([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
self.assertEqual(matrix.transposed(), expected)