[Python] Intuitive + Direct for Beginners with Illustrations
Given a matrix mat of 3 rows * 4 columns, \nwe want reshape it into 2 rows (r = 2) * 6 columns(c = 6): \n\n[[0,  1,  2,  3],\n [4,  5,  6,  7],                 
Given a matrix `mat` of 3 rows * 4 columns, \nwe want reshape it into 2 rows (`r = 2`) * 6 columns(`c = 6`): \n```\n[[0,  1,  2,  3],\n [4,  5,  6,  7],                 ->                [[0,  1,  2,  3,  4,  5],\n [8,  9,  10, 11]]                                   [6,  7,  8,  9,  10, 11]]\n```\n**Step 1:** Flatten the given matrix `mat` to a 1-D list `flatten` for easier visualizations and calculations for future steps\n```\nflatten = [0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11]\n```\n**Step 2:** Check whether the total number of given elements `len(flatten)` and the new shape `r * c` matches (i.e. whether "*given parameters is possible and legal*")\n```\nr = 2, c = 6\nr * c = 12\n```\n**Step 3:** Rearrange all elements in 1-D list `flatten` into the `new_mat`  according to given number of row `r` and given number of columns `c`\n```\nflatten = [0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11]\n```\n\nreshaping: \n```\n                         [0,  1,  2,  3,  4,  5,   6,  7,  8,  9,  10, 11]\nrow_index: 0\t\t\t| c0  c1  c2  c3  c4  c5 |\nrow_index: 1                           \t\t\t | c0  c1  c2  c3  c4  c5 |\n\nindex in flatten:  [row_index * c : row_index * c + c])\n\n\t\t\t            [0*6       :      0*6 + 6],  \n\t\t\t\t\t\t\t\t\t\t         [1*6        :       1*6+6]\n\t\t\t\t\t\t\t\t\t\t\t\t \n                        [0          :           6],  \n\t\t\t\t\t\t\t\t\t\t         [6          :          12]\n```\n- **iteration 0**:` row_index` **0**\n```\nflatten[0 : 6]   ->   [0,  1,  2,  3,  4,  5]\n\nappending:\nnew_mat = [[0,  1,  2,  3,  4,  5]]\n```\n- **iteration 1**: `row_index` **1**\n```\nflatten[6 : 12]   ->   [6,  7,  8,  9,  10, 11]\n\nappending:\nnew_mat = [[0,  1,  2,  3,  4,  5],\n           [6,  7,  8,  9,  10, 11]]\n```\n\n\n\n___________________________\n```\nclass Solution:\n    def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:\n        flatten = []\n        new_mat = []\n        for row in mat:\n            for num in row:\n                flatten.append(num)\n                \n        if r * c != len(flatten):   # when given parameters is NOT possible and legal\n            return mat\n        else:\n            for row_index in range(r):\n                new_mat.append(flatten[row_index * c : row_index * c + c])\n            return new_mat\n```\n\n________________________________________________________________________\n\nAs a total beginner, I am writing these all out to help myself, and hopefully also help anyone out there who is like me at the same time.\n\nPlease upvote\u2B06\uFE0F if you find this helpful or worth-reading for beginners in anyway.  \nYour upvote is much more than just supportive to me. \uD83D\uDE33\uD83E\uDD13\uD83E\uDD70\n\nIf you find this is not helpful, needs improvement, or is questionable, would you please leave a quick comment below to point out the problem before you decide to downvote? It will be very helpful for me (maybe also others) to learn as a beginner.\n\nThank you very much either way \uD83E\uDD13.\n
['Matrix', 'Python', 'Python3']