Skip to content

Commit a8dfd40

Browse files
edy252cclauss
andauthored
Add new algorithm index_2d_array_in_1d (#10973)
* Add new algorithm index_2d_array_in_1d * Add doctest for iter function * The power of dataclasses * Update index_2d_array_in_1d.py --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 34b25c0 commit a8dfd40

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Retrieves the value of an 0-indexed 1D index from a 2D array.
3+
There are two ways to retrieve value(s):
4+
5+
1. Index2DArrayIterator(matrix) -> Iterator[int]
6+
This iterator allows you to iterate through a 2D array by passing in the matrix and
7+
calling next(your_iterator). You can also use the iterator in a loop.
8+
Examples:
9+
list(Index2DArrayIterator(matrix))
10+
set(Index2DArrayIterator(matrix))
11+
tuple(Index2DArrayIterator(matrix))
12+
sum(Index2DArrayIterator(matrix))
13+
-5 in Index2DArrayIterator(matrix)
14+
15+
2. index_2d_array_in_1d(array: list[int], index: int) -> int
16+
This function allows you to provide a 2D array and a 0-indexed 1D integer index,
17+
and retrieves the integer value at that index.
18+
19+
Python doctests can be run using this command:
20+
python3 -m doctest -v index_2d_array_in_1d.py
21+
"""
22+
23+
from collections.abc import Iterator
24+
from dataclasses import dataclass
25+
26+
27+
@dataclass
28+
class Index2DArrayIterator:
29+
matrix: list[list[int]]
30+
31+
def __iter__(self) -> Iterator[int]:
32+
"""
33+
>>> tuple(Index2DArrayIterator([[5], [-523], [-1], [34], [0]]))
34+
(5, -523, -1, 34, 0)
35+
>>> tuple(Index2DArrayIterator([[5, -523, -1], [34, 0]]))
36+
(5, -523, -1, 34, 0)
37+
>>> tuple(Index2DArrayIterator([[5, -523, -1, 34, 0]]))
38+
(5, -523, -1, 34, 0)
39+
>>> t = Index2DArrayIterator([[5, 2, 25], [23, 14, 5], [324, -1, 0]])
40+
>>> tuple(t)
41+
(5, 2, 25, 23, 14, 5, 324, -1, 0)
42+
>>> list(t)
43+
[5, 2, 25, 23, 14, 5, 324, -1, 0]
44+
>>> sorted(t)
45+
[-1, 0, 2, 5, 5, 14, 23, 25, 324]
46+
>>> tuple(t)[3]
47+
23
48+
>>> sum(t)
49+
397
50+
>>> -1 in t
51+
True
52+
>>> t = iter(Index2DArrayIterator([[5], [-523], [-1], [34], [0]]))
53+
>>> next(t)
54+
5
55+
>>> next(t)
56+
-523
57+
"""
58+
for row in self.matrix:
59+
yield from row
60+
61+
62+
def index_2d_array_in_1d(array: list[list[int]], index: int) -> int:
63+
"""
64+
Retrieves the value of the one-dimensional index from a two-dimensional array.
65+
66+
Args:
67+
array: A 2D array of integers where all rows are the same size and all
68+
columns are the same size.
69+
index: A 1D index.
70+
71+
Returns:
72+
int: The 0-indexed value of the 1D index in the array.
73+
74+
Examples:
75+
>>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], 5)
76+
5
77+
>>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], -1)
78+
Traceback (most recent call last):
79+
...
80+
ValueError: index out of range
81+
>>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], 12)
82+
Traceback (most recent call last):
83+
...
84+
ValueError: index out of range
85+
>>> index_2d_array_in_1d([[]], 0)
86+
Traceback (most recent call last):
87+
...
88+
ValueError: no items in array
89+
"""
90+
rows = len(array)
91+
cols = len(array[0])
92+
93+
if rows == 0 or cols == 0:
94+
raise ValueError("no items in array")
95+
96+
if index < 0 or index >= rows * cols:
97+
raise ValueError("index out of range")
98+
99+
return array[index // cols][index % cols]
100+
101+
102+
if __name__ == "__main__":
103+
import doctest
104+
105+
doctest.testmod()

0 commit comments

Comments
 (0)