Python - One Line, Two Line, Yield, Generator, Circular Index, Numpy - with explaination(#556)
Solution #1: Using Numpy\nBelow is the one line code which makes use of numpy.reshape method provided by numpy.\npython\nimport numpy\nclass Solution:\n    def 
# Solution #1: Using Numpy\nBelow is the one line code which makes use of numpy.reshape method provided by numpy.\n```python\nimport numpy\nclass Solution:\n    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n        return numpy.reshape(mat,(r,c)) if r*c==len(mat)*len(mat[0]) else mat\n```\n## Lets look into the breadcrumbs!!\n> NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.\n> [<<\\<Wikipedia\\>>>](https://en.wikipedia.org/wiki/NumPy)\n```python\nreturn A if B else C\n```\nThis kind of statement is similar to ternary operator. Returns `A` if `B` evaluates to `True`, if not return `C`.\n```python\nnumpy.reshape(matrix, (row,col))\n```\n> Above method gives a new shape to an array without changing its data.\n>  [<<\\<NumPy Docs\\>>>](https://numpy.org/doc/stable/reference/generated/numpy.reshape.html)\n```python\nif r*c==len(mat)*len(mat[0])\n```\nCondition: Check if the size of matrix is equals to the size of the matrix which is to be returned.\n```\nlen(mat) # gives the number of rows\nlen(mat[0]) # gives the number of columns\n```\n-------------------------\n\n# Solution #2: Using Queue\nBelow solution doesnt use any other libraries. But its written in two lines and makes use of another list/queue to build a matrix.\n```python\nclass Solution:\n    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n        queue = [cell for row in mat for cell in row] if r*c==len(mat)*len(mat[0]) else []\n        return [[queue.pop(0) for _ in range(c)] for _ in range(r)] if queue else mat\n```\n## Lets look into the breadcrumbs!!\n```python\nqueue = [cell for row in mat for cell in row] if r*c==len(mat)*len(mat[0]) else []\n```\n`queue` contains the flattened matrix (flattening from 2D to 1D, using [row-major](https://en.wikipedia.org/wiki/Row-_and_column-major_order)). However speaking of memory, matrix is stored as 1D array in memory.\n`queue` will be empty if the size of given matrix and matrix supposed to be created does not match.\n```python\n [[queue.pop(0) for _ in range(c)] for _ in range(r)] if queue else mat\n```\nUsing row-major itself, we are creating a new matrix with new row_no and col_no, and finally return it.\n\n-----------------------------\n# Solution #3: Using Circular Index\nAnother two line solution which does not use any extra space or extrnal packages.\n```python\nclass Solution:\n    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n        if r*c!=len(mat)*(cols:=len(mat[0])): return mat\n        return [[mat[(i*c+j)//cols][(i*c+j)%cols] for j in range(c)] for i in range(r)]\n```\n## Lets look into the breadcrumbs!!\nAny 2D matrix will be stored as stated below on machines follwing row-major order.\n![image](https://assets.leetcode.com/users/images/f54a0c80-d9cf-4d6f-bfe4-80188a10977d_1625498161.5667083.png)\n[<<\\<Image Source\\>>>](https://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays)\n[To know more, please make use of this webpage](https://eli.thegreenplace.net/2015/memory-layout-of-multi-dimensional-arrays)\n```python\nif r*c!=len(mat)*(cols:=len(mat[0])): return mat\n```\nReturn the given matrix if the size does not match\n```python\n(cols:=len(mat[0]) # this is an assignment expression\n```\nTo know more on assignment expressions, [click here](https://realpython.com/lessons/assignment-expressions/ "Real Python")\nWe are assigning `cols` the number of columns available in given matrix and returning the same value in one single line.\n```python\n(i*c+j) # conversion from row,column to offset\n(i*c+j)//cols # gives the row number of old matrix\n(i*c+j)%cols # givens the column number of old matrix\nmat[(i*c+j)//cols][(i*c+j)%cols] # gives you the value at specified row and column\n```\n\n-----------------------------\n# Solution #4: Using Yield\n```python\nclass Solution:\n    def getCell(self, mat):\n        for row in mat:\n            for cell in row:\n                yield cell\n    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n        gen = self.getCell(mat)\n        return [[next(gen) for _ in range(c)] for _ in range(r)] if len(mat) * len(mat[0]) == r * c else mat\n```\n## Lets look into the breadcrumbs!!\n```python\ndef getCell(self, mat):\n\tfor row in mat:\n\t\tfor cell in row:\n\t\t\tyield cell\n```\n`getCell()` is a generator method which returns the next element in the original/old matrix.\nTo know about generators, [click here](https://realpython.com/introduction-to-python-generators/)\n```python\n[[next(gen) for _ in range(c)] for _ in range(r)] if len(mat) * len(mat[0]) == r * c else mat\n```\nCreate a new matrix and return it if the condition of size satisfies, else return the old matrix.\n\n-----------------------------\n# Solution #5: Using Generator\n```python\nclass Solution:\n    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n        gen = (cell for row in mat for cell in row)\n        return [[next(gen) for _ in range(c)] for _ in range(r)] if len(mat) * len(mat[0]) == r * c else mat\n```\n## Lets look into the breadcrumbs!!\n```python\ngen = (cell for row in mat for cell in row)\n```\nIn the above solution (Solution#4), we used a generator method to generate next elements. Instead of writing a function, lets use generator expression. The only difference between Solution#4 and Solution#5 is that, 4 uses generator method and 5 uses generator expression.\n\n---------\nI hope this is helpful to you :)\n\n--------
['Queue', 'Python', 'Python3']