Easy Python | 100% Speed | Yield Method
Easy Python | 100% Speed | Yield Method\n\nClean Python algorithm building the output array in a very easy way :)\n\n\nclass Solution:\n    def yielder(self,A):
**Easy Python | 100% Speed | Yield Method**\n\nClean Python algorithm building the output array in a very easy way :)\n\n```\nclass Solution:\n    def yielder(self,A):\n        for row in A:\n            for x in row:\n                yield x\n    def matrixReshape(self, A, r, c):\n        if not A:\n            return []\n        if len(A)*len(A[0]) != r*c:\n            return A\n        #\n        gen = self.yielder(A)\n        return [ [ next(gen) for _ in range(c) ] for _ in range(r) ]\n```\n\n**PS. Circular Index Method**\n\nThe Yield Method works amazingly well in Python. However, implementing anything resembling the Yield functionality is low-level languages is very cumbersome (if at all possible). The Circular Index Method is a much cleaner algorithm compatible with all programming languages:\n```\nclass Solution:\n    def matrixReshape(self, A, r, c):\n        if not A:\n            return []\n        m,n = len(A),len(A[0])\n        if m*n != r*c:\n            return A\n        #\n        B = []\n        for i in range(r):\n            row = []\n            for j in range(c):\n                k = i*c + j\n                row.append( A[k//n][k%n] )\n            B.append( row )\n        return B\n```\n
['Python', 'Python3']