-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Add new algorithm index_2d_array_in_1d #10973
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
""" | ||
Retrieves the value of an 0-indexed 1D index from a 2D array. | ||
There are two ways to retrieve value(s): | ||
|
||
1. index_2d_array_in_1d(array: list[int], index: int) -> int | ||
This function allows you to provide a 2D array and a 0-indexed 1D integer index, | ||
and retrieves the integer value at that index. | ||
|
||
2. Index2DArrayIterator(array) -> Iterator[int] | ||
This iterator allows you to iterate through a 2D array by passing in the array and | ||
calling __next__() on your iterator. You can also use the iterator in a loop. | ||
Example: | ||
[value for value in Index2DArrayIterator(array)] | ||
|
||
Python doctests can be run using this command: | ||
python3 -m doctest -v index_2d_array_in_1d.py | ||
""" | ||
|
||
from collections.abc import Iterator | ||
|
||
|
||
class Index2DArrayIterator: | ||
""" | ||
Creates an iterator to iterate through the values in the given 2D array. | ||
|
||
Args: | ||
array (List[int]): A 2D array of integers where all rows are the same size | ||
and all columns are the same size. | ||
""" | ||
|
||
def __init__(self, array: list[list[int]]): | ||
self.array = array | ||
self.cols = len(array[0]) | ||
self.n = len(array) * len(array[0]) | ||
self.i = 0 | ||
|
||
def __iter__(self) -> Iterator[int]: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self | ||
|
||
def __next__(self) -> int: | ||
""" | ||
Returns the next item in the array. | ||
|
||
Returns: | ||
int: Value in array. | ||
|
||
>>> t = Index2DArrayIterator([[5],[-523],[-1],[34],[0]]) | ||
>>> t.__next__() | ||
5 | ||
>>> t.__next__() | ||
-523 | ||
>>> u = Index2DArrayIterator([[5, 2, 25], [23, 14, 5], [324, -1, 0]]) | ||
>>> [val for val in u] | ||
[5, 2, 25, 23, 14, 5, 324, -1, 0] | ||
""" | ||
if self.i < self.n: | ||
x = self.array[self.i // self.cols][self.i % self.cols] | ||
self.i += 1 | ||
return x | ||
else: | ||
raise StopIteration | ||
|
||
|
||
def index_2d_array_in_1d(array: list[list[int]], index: int) -> int: | ||
""" | ||
Retrieves the value of the one-dimensional index from a two-dimensional array. | ||
|
||
Args: | ||
array (List[int]): A 2D array of integers where all rows are the same size and | ||
all columns are the same size. | ||
index: A 1D index. | ||
|
||
Returns: | ||
int: The 0-indexed value of the 1D index in the array. | ||
|
||
Examples: | ||
>>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], 5) | ||
5 | ||
>>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], -1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: index out of range | ||
>>> index_2d_array_in_1d([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]], 12) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: index out of range | ||
>>> index_2d_array_in_1d([[]], 0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: no items in array | ||
""" | ||
rows = len(array) | ||
cols = len(array[0]) | ||
|
||
if rows == 0 or cols == 0: | ||
raise ValueError("no items in array") | ||
|
||
if index < 0 or index >= rows * cols: | ||
raise ValueError("index out of range") | ||
|
||
return array[index // cols][index % cols] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None: