retrieves the value of an 0indexed 1d index from a 2d array there are two ways to retrieve values 1 index2darrayiteratormatrix iteratorint this iterator allows you to iterate through a 2d array by passing in the matrix and calling nextyouriterator you can also use the iterator in a loop examples listindex2darrayiteratormatrix setindex2darrayiteratormatrix tupleindex2darrayiteratormatrix sumindex2darrayiteratormatrix 5 in index2darrayiteratormatrix 2 index2darrayin1darray listint index int int this function allows you to provide a 2d array and a 0indexed 1d integer index and retrieves the integer value at that index python doctests can be run using this command python3 m doctest v index2darrayin1d py tupleindex2darrayiterator5 523 1 34 0 5 523 1 34 0 tupleindex2darrayiterator5 523 1 34 0 5 523 1 34 0 tupleindex2darrayiterator5 523 1 34 0 5 523 1 34 0 t index2darrayiterator5 2 25 23 14 5 324 1 0 tuplet 5 2 25 23 14 5 324 1 0 listt 5 2 25 23 14 5 324 1 0 sortedt 1 0 2 5 5 14 23 25 324 tuplet3 23 sumt 397 1 in t true t iterindex2darrayiterator5 523 1 34 0 nextt 5 nextt 523 retrieves the value of the onedimensional index from a twodimensional array args array 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 0indexed value of the 1d index in the array examples index2darrayin1d0 1 2 3 4 5 6 7 8 9 10 11 5 5 index2darrayin1d0 1 2 3 4 5 6 7 8 9 10 11 1 traceback most recent call last valueerror index out of range index2darrayin1d0 1 2 3 4 5 6 7 8 9 10 11 12 traceback most recent call last valueerror index out of range index2darrayin1d 0 traceback most recent call last valueerror no items in array tuple index2darrayiterator 5 523 1 34 0 5 523 1 34 0 tuple index2darrayiterator 5 523 1 34 0 5 523 1 34 0 tuple index2darrayiterator 5 523 1 34 0 5 523 1 34 0 t index2darrayiterator 5 2 25 23 14 5 324 1 0 tuple t 5 2 25 23 14 5 324 1 0 list t 5 2 25 23 14 5 324 1 0 sorted t 1 0 2 5 5 14 23 25 324 tuple t 3 23 sum t 397 1 in t true t iter index2darrayiterator 5 523 1 34 0 next t 5 next t 523 retrieves the value of the one dimensional index from a two dimensional array args array 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
from collections.abc import Iterator
from dataclasses import dataclass


@dataclass
class Index2DArrayIterator:
    matrix: list[list[int]]

    def __iter__(self) -> Iterator[int]:
        
        for row in self.matrix:
            yield from row


def index_2d_array_in_1d(array: list[list[int]], index: int) -> int:
    
    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()