Skip to content

Commit 65719fd

Browse files
committed
Add new algorithm index_2d_array_in_1d
1 parent 76acc6d commit 65719fd

File tree

1 file changed

+107
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)