diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-02-13 11:36:49 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-02-13 11:36:49 +0100 |
| commit | 954b2ff314f57c9b76d07464589359ffec42f5fb (patch) | |
| tree | 2b45c48792a1a3bb39fed5470f0bdec429ec840d /challenge-204/lubos-kolouch/python | |
| parent | 4560012437d1aa5816303b29b5fe3d9f5f936969 (diff) | |
| download | perlweeklychallenge-club-954b2ff314f57c9b76d07464589359ffec42f5fb.tar.gz perlweeklychallenge-club-954b2ff314f57c9b76d07464589359ffec42f5fb.tar.bz2 perlweeklychallenge-club-954b2ff314f57c9b76d07464589359ffec42f5fb.zip | |
Challenge 204 Task2 LK Perl Python
Diffstat (limited to 'challenge-204/lubos-kolouch/python')
| -rw-r--r-- | challenge-204/lubos-kolouch/python/ch-2.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-204/lubos-kolouch/python/ch-2.py b/challenge-204/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..4a49b7a544 --- /dev/null +++ b/challenge-204/lubos-kolouch/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import numpy as np +from typing import List, Union + + +def reshape(matrix: List[List[int]], r: int, + c: int) -> Union[int, List[List[int]]]: + m, n = np.shape(matrix) + if m * n != r * c: + return 0 + flat_matrix = np.array(matrix).flatten() + reshaped_matrix = np.reshape(flat_matrix, (r, c)) + return reshaped_matrix.tolist() + + +matrix = [[1, 2], [3, 4]] +r = 1 +c = 4 +result = reshape(matrix, r, c) + +if result == 0: + print("Cannot reshape matrix") +else: + print("Reshaped matrix:") + for row in result: + print(row) |
