From b405b34974d121efffd2b0d46a468f1f2c7b8dcd Mon Sep 17 00:00:00 2001 From: slowy07 Date: Thu, 26 Aug 2021 09:36:06 +0700 Subject: [PATCH 01/11] [fixed] unused variable, standalone running, import doctest module information [standalone running](https://www.geeksforgeeks.org/what-does-the-if-__name__-__main__-do/) Signed-off-by: slowy07 --- matrix/nth_fibonacci_using_matrix_exponentiation.py | 3 +++ matrix/spiral_print.py | 5 +++-- other/fischer_yates_shuffle.py | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/matrix/nth_fibonacci_using_matrix_exponentiation.py b/matrix/nth_fibonacci_using_matrix_exponentiation.py index 8c39de0f23b6..341a02e1a95d 100644 --- a/matrix/nth_fibonacci_using_matrix_exponentiation.py +++ b/matrix/nth_fibonacci_using_matrix_exponentiation.py @@ -88,4 +88,7 @@ def main(): if __name__ == "__main__": + import doctest + + doctest.testmod() main() diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 21dab76156e9..df92ef11fad6 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -64,5 +64,6 @@ def spiralPrint(a): # driver code -a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] -spiralPrint(a) +if __name__ == "__main__": + a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] + spiralPrint(a) diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index 6eec738c02e1..a9f311f9da92 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -9,7 +9,7 @@ def FYshuffle(list): - for i in range(len(list)): + for _ in range(len(list)): a = random.randint(0, len(list) - 1) b = random.randint(0, len(list) - 1) list[a], list[b] = list[b], list[a] From 7bb24108866e4e616e3e6d39e26e51f62e2889a0 Mon Sep 17 00:00:00 2001 From: arfy slowy Date: Fri, 27 Aug 2021 22:36:03 +0700 Subject: [PATCH 02/11] Update other/fischer_yates_shuffle.py Co-authored-by: Christian Clauss --- other/fischer_yates_shuffle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index a9f311f9da92..181c01b088c9 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -8,7 +8,7 @@ import random -def FYshuffle(list): +def fisher_yates_shuffle(data: list) -> list: for _ in range(len(list)): a = random.randint(0, len(list) - 1) b = random.randint(0, len(list) - 1) From ffdd4aaeabb00f22186f7718efc189b2c10fb82a Mon Sep 17 00:00:00 2001 From: slowy07 Date: Fri, 27 Aug 2021 22:53:34 +0700 Subject: [PATCH 03/11] [fixed] change to tuple and fixing callfunction Signed-off-by: slowy07 --- matrix/spiral_print.py | 8 ++++---- other/fischer_yates_shuffle.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index df92ef11fad6..846e80ae690f 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -10,8 +10,8 @@ def checkMatrix(a): # must be - if type(a) == list and len(a) > 0: - if type(a[0]) == list: + if type(a) == tuple and len(a) > 0: + if type(a[0]) == tuple: prevLen = 0 for i in a: if prevLen == 0: @@ -33,7 +33,7 @@ def spiralPrint(a): if checkMatrix(a) and len(a) > 0: matRow = len(a) - if type(a[0]) == list: + if type(a[0]) == tuple: matCol = len(a[0]) else: for dat in a: @@ -65,5 +65,5 @@ def spiralPrint(a): # driver code if __name__ == "__main__": - a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] + a = ([1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]) spiralPrint(a) diff --git a/other/fischer_yates_shuffle.py b/other/fischer_yates_shuffle.py index 181c01b088c9..035fcb482380 100644 --- a/other/fischer_yates_shuffle.py +++ b/other/fischer_yates_shuffle.py @@ -21,4 +21,4 @@ def fisher_yates_shuffle(data: list) -> list: strings = ["python", "says", "hello", "!"] print("Fisher-Yates Shuffle:") print("List", integers, strings) - print("FY Shuffle", FYshuffle(integers), FYshuffle(strings)) + print("FY Shuffle", fisher_yates_shuffle(integers), fisher_yates_shuffle(strings)) From ef3ea0e0d6739d3a6dffd79452162a1269fc4396 Mon Sep 17 00:00:00 2001 From: arfy slowy Date: Sat, 28 Aug 2021 20:01:36 +0700 Subject: [PATCH 04/11] Update matrix/spiral_print.py Co-authored-by: Christian Clauss --- matrix/spiral_print.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 846e80ae690f..63b1a4d21082 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -33,7 +33,7 @@ def spiralPrint(a): if checkMatrix(a) and len(a) > 0: matRow = len(a) - if type(a[0]) == tuple: + if isinstance(a[0], Iterable): matCol = len(a[0]) else: for dat in a: From e6527ab186354a5738b5f054dac3d06a14e559b7 Mon Sep 17 00:00:00 2001 From: arfy slowy Date: Sat, 28 Aug 2021 20:01:57 +0700 Subject: [PATCH 05/11] Update matrix/spiral_print.py Co-authored-by: Christian Clauss --- matrix/spiral_print.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 63b1a4d21082..369aa317a183 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -7,7 +7,7 @@ ii)column of all the row should be equal """ - +from collections.abc import Iterable def checkMatrix(a): # must be if type(a) == tuple and len(a) > 0: From aa556b8edf2e20ceedd940049c2bd58cb40277b1 Mon Sep 17 00:00:00 2001 From: arfy slowy Date: Sat, 28 Aug 2021 21:26:57 +0700 Subject: [PATCH 06/11] fixing Co-authored-by: Christian Clauss --- matrix/spiral_print.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 369aa317a183..b4a0ceb700aa 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -10,8 +10,7 @@ from collections.abc import Iterable def checkMatrix(a): # must be - if type(a) == tuple and len(a) > 0: - if type(a[0]) == tuple: + if a and if isinstance(a, Iterable): prevLen = 0 for i in a: if prevLen == 0: From 8fc14026089cf6d8237b88ecb0c409a7617ef6e7 Mon Sep 17 00:00:00 2001 From: slowy07 Date: Sat, 28 Aug 2021 21:35:09 +0700 Subject: [PATCH 07/11] [fixed] sprial matrix Signed-off-by: slowy07 --- matrix/spiral_print.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index b4a0ceb700aa..2d6fba37daaa 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -8,22 +8,23 @@ """ from collections.abc import Iterable + + def checkMatrix(a): # must be - if a and if isinstance(a, Iterable): - prevLen = 0 - for i in a: - if prevLen == 0: - prevLen = len(i) - result = True - elif prevLen == len(i): - result = True - else: - result = False - else: - result = True + if a and isinstance(a, Iterable): + prevLen = 0 + for i in a: + if prevLen == 0: + prevLen = len(i) + result = True + elif prevLen == len(i): + result = True + else: + result = False else: - result = False + result = True + return result From cd8cf0d32ce03fdc0c0de9e96af9c8ed1dc99ef6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Aug 2021 19:29:00 +0200 Subject: [PATCH 08/11] Update spiral_print.py --- matrix/spiral_print.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 2d6fba37daaa..af648304ccab 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -4,22 +4,23 @@ Matrix must satisfy below conditions i) matrix should be only one or two dimensional - ii)column of all the row should be equal + ii) column of all rows should be equal """ from collections.abc import Iterable -def checkMatrix(a): +def check_matrix(matrix): # must be - if a and isinstance(a, Iterable): - prevLen = 0 - for i in a: - if prevLen == 0: - prevLen = len(i) - result = True - elif prevLen == len(i): - result = True + if matrix and isinstance(matrix, Iterable): + if isinstance(matrix[0], Iterable): + prev_len = 0 + for row in matrix: + if prev_len == 0: + prev_len = len(row) + result = True + else: + result = prev_len == len(row): else: result = False else: From 422a798ca53449169d654eef1027db1f399abdbc Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Aug 2021 19:34:13 +0200 Subject: [PATCH 09/11] Update spiral_print.py --- matrix/spiral_print.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index af648304ccab..70b2339fd0b8 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -21,10 +21,10 @@ def check_matrix(matrix): result = True else: result = prev_len == len(row): - else: - result = False + else: + result = True else: - result = True + result = False return result From a65bce518fa6e5425aa09093263202b0fc02de18 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Aug 2021 19:37:20 +0200 Subject: [PATCH 10/11] Update spiral_print.py --- matrix/spiral_print.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 70b2339fd0b8..aa3c0ab2939a 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -20,7 +20,7 @@ def check_matrix(matrix): prev_len = len(row) result = True else: - result = prev_len == len(row): + result = prev_len == len(row) else: result = True else: From b451253b370808ec6887d5bfc4de0ffc9f5c5dbe Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 28 Aug 2021 19:44:44 +0200 Subject: [PATCH 11/11] Update spiral_print.py --- matrix/spiral_print.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index aa3c0ab2939a..6f699c1ab662 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -4,7 +4,7 @@ Matrix must satisfy below conditions i) matrix should be only one or two dimensional - ii) column of all rows should be equal + ii) number of column of all rows should be equal """ from collections.abc import Iterable @@ -30,9 +30,7 @@ def check_matrix(matrix): def spiralPrint(a): - - if checkMatrix(a) and len(a) > 0: - + if check_matrix(a) and len(a) > 0: matRow = len(a) if isinstance(a[0], Iterable): matCol = len(a[0])