From 670442edc2e2b9a4dbb9df9a40df24731af80c88 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:44:21 +0530 Subject: [PATCH 01/15] Update spiral_print.py --- matrix/spiral_print.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 0cf732d60ca8..9a9b3de0e8a6 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -7,6 +7,9 @@ """ +from typing import List + + def check_matrix(matrix: list[list[int]]) -> bool: # must be matrix = [list(row) for row in matrix] @@ -75,8 +78,67 @@ def spiral_print_clockwise(a: list[list[int]]) -> None: print("Not a valid matrix") return +# Other Easy to undersatnd Approach + + +def spiral_traversal(matrix: List[List[int]]) -> List[int]: + """ + >>> spiral_traversal([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) + [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + + Example: + matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] + + Algorithm: + Step 1. first pop the 0 index list. (which is [1,2,3,4] and concatenate the + output of [step 2]) + Step 2. Now perform matrix’s Transpose operation (Change rows to column + and vice versa) and reverse the resultant matrix. + Step 3. Pass the output of [2nd step], to same recursive function till + base case hits. + + Dry Run: + + Stage 1. + [1, 2, 3, 4] + spiral_traversal( [[8, 12 ] + [7, 11] + [6, 10] + [5, 9]] + ) + + Stage 2. + [1, 2, 3, 4, 8, 12] + spiral_traversal( [[11, 10, 9], + [7, 6, 5]] + ) + + Stage 3. + [1, 2, 3, 4, 8, 12, 11, 10, 9] + spiral_traversal( [[5], + [6], + [7] + ]) + + Stage 4. + [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal( [[5], + [6], + [7] + ]) + + Stage 5. + [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal( [[6, 7] + ]) + + Stage 6. + [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) + + + """ + return matrix and list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) + # driver code if __name__ == "__main__": + import doctest + + doctest.testmod() a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] spiral_print_clockwise(a) From a45507e4af2745700424e128d82e3cc08652a054 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:18:24 +0000 Subject: [PATCH 02/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/spiral_print.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 9a9b3de0e8a6..6dee932e34d8 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -78,10 +78,11 @@ def spiral_print_clockwise(a: list[list[int]]) -> None: print("Not a valid matrix") return + # Other Easy to undersatnd Approach -def spiral_traversal(matrix: List[List[int]]) -> List[int]: +def spiral_traversal(matrix: list[list[int]]) -> list[int]: """ >>> spiral_traversal([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] From d0b5a8a424aca17be3643645ac2e32984e07c6a6 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:55:11 +0530 Subject: [PATCH 03/15] Update matrix/spiral_print.py Co-authored-by: Caeden Perelli-Harris --- matrix/spiral_print.py | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 6dee932e34d8..2ab04d8f611d 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -141,5 +141,6 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: import doctest doctest.testmod() + a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] spiral_print_clockwise(a) From f9f7a74f90f8973b1c90b9350034115989013a7e Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:55:23 +0530 Subject: [PATCH 04/15] Update matrix/spiral_print.py Co-authored-by: Caeden Perelli-Harris --- matrix/spiral_print.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 2ab04d8f611d..bc59c7c6b2f3 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -130,8 +130,6 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: Stage 6. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) - - """ return matrix and list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) From 506bcd0d07d562351531add399fd406becdcaf03 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:55:34 +0530 Subject: [PATCH 05/15] Update matrix/spiral_print.py Co-authored-by: Caeden Perelli-Harris --- matrix/spiral_print.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index bc59c7c6b2f3..a2b8397d0885 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -101,32 +101,27 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: Dry Run: Stage 1. - [1, 2, 3, 4] + spiral_traversal( [[8, 12 ] - [7, 11] - [6, 10] - [5, 9]] - ) + [1, 2, 3, 4] + spiral_traversal([ + [8, 12], [7, 11], [6, 10], [5, 9]] + ]) Stage 2. - [1, 2, 3, 4, 8, 12] + spiral_traversal( [[11, 10, 9], - [7, 6, 5]] - ) + [1, 2, 3, 4, 8, 12] + spiral_traversal([ + [11, 10, 9], [7, 6, 5] + ]) Stage 3. - [1, 2, 3, 4, 8, 12, 11, 10, 9] + spiral_traversal( [[5], - [6], - [7] - ]) + [1, 2, 3, 4, 8, 12, 11, 10, 9] + spiral_traversal([ + [5], [6], [7] + ]) Stage 4. - [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal( [[5], - [6], - [7] - ]) + [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal([ + [5], [6], [7] + ]) Stage 5. - [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal( [[6, 7] - ]) + [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal([[6, 7]]) Stage 6. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) From 3722730cc442901f7790c64331a1beaaceb75201 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:55:48 +0530 Subject: [PATCH 06/15] Update matrix/spiral_print.py Co-authored-by: Caeden Perelli-Harris --- matrix/spiral_print.py | 1 - 1 file changed, 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index a2b8397d0885..c4a375a568d1 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -7,7 +7,6 @@ """ -from typing import List def check_matrix(matrix: list[list[int]]) -> bool: From 2ec819184d1b61be1e57fa9165dbec8dbc5cb1be Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 14:28:07 +0000 Subject: [PATCH 07/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/spiral_print.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index c4a375a568d1..1ef3daea2e7e 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -7,8 +7,6 @@ """ - - def check_matrix(matrix: list[list[int]]) -> bool: # must be matrix = [list(row) for row in matrix] @@ -133,6 +131,6 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: import doctest doctest.testmod() - + a = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] spiral_print_clockwise(a) From 9cef49d691da58183e9a90480347bc128fa58c53 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:48:19 +0530 Subject: [PATCH 08/15] Update spiral_print.py --- matrix/spiral_print.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 1ef3daea2e7e..1844dc7fdd5b 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -123,7 +123,9 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: Stage 6. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) """ - return matrix and list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) + if matrix: + spiral_traverse_mat = list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) + return spiral_traverse_mat # driver code From d8110e9cb0c68aa4f2693a7bbd5a667e6c7730bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 15:19:10 +0000 Subject: [PATCH 09/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/spiral_print.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 1844dc7fdd5b..03a89d049685 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -124,7 +124,9 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) """ if matrix: - spiral_traverse_mat = list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) + spiral_traverse_mat = list(matrix.pop(0)) + spiral_traversal( + list(zip(*matrix))[::-1] + ) return spiral_traverse_mat From 779b8087ba97ec0bc6b4c2d2e1a287e872e1d018 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 20:52:52 +0530 Subject: [PATCH 10/15] 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 03a89d049685..d409e5019431 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -123,11 +123,9 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: Stage 6. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) """ - if matrix: - spiral_traverse_mat = list(matrix.pop(0)) + spiral_traversal( - list(zip(*matrix))[::-1] + return list(matrix.pop(0)) + spiral_traversal( + list(map(list,list(zip(*matrix))))[::-1] ) - return spiral_traverse_mat # driver code From 050f3a33e29a7f20d8a5dcbb918520e22e110ac0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Oct 2022 15:24:02 +0000 Subject: [PATCH 11/15] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/spiral_print.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index d409e5019431..7de11b9d0cf5 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -124,8 +124,8 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) """ return list(matrix.pop(0)) + spiral_traversal( - list(map(list,list(zip(*matrix))))[::-1] - ) + list(map(list, list(zip(*matrix))))[::-1] + ) # driver code From 399d9f4c1e6a7eb091d11ba2e881e287a703031a Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:21:58 +0530 Subject: [PATCH 12/15] Update spiral_print.py --- matrix/spiral_print.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 7de11b9d0cf5..0a7d2b022444 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -7,6 +7,9 @@ """ +from typing import Any + + def check_matrix(matrix: list[list[int]]) -> bool: # must be matrix = [list(row) for row in matrix] @@ -79,7 +82,7 @@ def spiral_print_clockwise(a: list[list[int]]) -> None: # Other Easy to undersatnd Approach -def spiral_traversal(matrix: list[list[int]]) -> list[int]: +def spiral_traversal(matrix: list[list]) -> list[int]: """ >>> spiral_traversal([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]) [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] @@ -123,9 +126,8 @@ def spiral_traversal(matrix: list[list[int]]) -> list[int]: Stage 6. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) """ - return list(matrix.pop(0)) + spiral_traversal( - list(map(list, list(zip(*matrix))))[::-1] - ) + matrix = matrix or [[]] + return list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) # driver code From c3d48e7021efe4dd4b93f32c561f139f7e451b2f Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:27:49 +0530 Subject: [PATCH 13/15] Update spiral_print.py --- matrix/spiral_print.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 0a7d2b022444..99e23b3b45a5 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -7,9 +7,6 @@ """ -from typing import Any - - def check_matrix(matrix: list[list[int]]) -> bool: # must be matrix = [list(row) for row in matrix] From e0513bb0aa74ffaa528e270a348ca54fcc2c2d00 Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 22:12:13 +0530 Subject: [PATCH 14/15] Update spiral_print.py --- matrix/spiral_print.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 99e23b3b45a5..0449cd557d22 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -86,7 +86,6 @@ def spiral_traversal(matrix: list[list]) -> list[int]: Example: matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] - Algorithm: Step 1. first pop the 0 index list. (which is [1,2,3,4] and concatenate the output of [step 2]) @@ -94,37 +93,32 @@ def spiral_traversal(matrix: list[list]) -> list[int]: and vice versa) and reverse the resultant matrix. Step 3. Pass the output of [2nd step], to same recursive function till base case hits. - Dry Run: - Stage 1. [1, 2, 3, 4] + spiral_traversal([ [8, 12], [7, 11], [6, 10], [5, 9]] ]) - Stage 2. [1, 2, 3, 4, 8, 12] + spiral_traversal([ [11, 10, 9], [7, 6, 5] ]) - Stage 3. [1, 2, 3, 4, 8, 12, 11, 10, 9] + spiral_traversal([ [5], [6], [7] ]) - Stage 4. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal([ [5], [6], [7] ]) - Stage 5. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5] + spiral_traversal([[6, 7]]) - Stage 6. [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7] + spiral_traversal([]) """ - matrix = matrix or [[]] - return list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) + if matrix: + return list(matrix.pop(0)) + spiral_traversal(list(zip(*matrix))[::-1]) + else: + return [] # driver code From fb56b95bee9e511cedf95b2442796bff4cc8dc0d Mon Sep 17 00:00:00 2001 From: Shubham Kondekar <40213815+kondekarshubham123@users.noreply.github.com> Date: Wed, 26 Oct 2022 22:22:10 +0530 Subject: [PATCH 15/15] 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 0449cd557d22..0d0be1527aec 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -76,7 +76,7 @@ def spiral_print_clockwise(a: list[list[int]]) -> None: return -# Other Easy to undersatnd Approach +# Other Easy to understand Approach def spiral_traversal(matrix: list[list]) -> list[int]: