From 6abbd170e45adf6e02c818b63f7a8ff8855f460d Mon Sep 17 00:00:00 2001 From: Bradley Small <vc.Bradley.Small@lowes.com> Date: Fri, 15 Nov 2019 14:17:30 -0500 Subject: [PATCH] first blush --- README.md | 3 +++ snail.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 snail.py diff --git a/README.md b/README.md index e69de29..2d1cf83 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,3 @@ +Snail Sort + +This is a matrix manipulation routine that flattens a matrix to a string in a clockwise spiral. diff --git a/snail.py b/snail.py new file mode 100644 index 0000000..5b71543 --- /dev/null +++ b/snail.py @@ -0,0 +1,39 @@ +"""Demonstrate the snail sort.""" + + +def snail_sort(matrix): + """Unwind a matrix clockwise into a string.""" + new_list = [] + + while matrix: + new_list.extend(matrix.pop(0)) + if not matrix: + continue + + for i in range(len(matrix)): + new_list.append(matrix[i].pop()) + if not matrix: + break + if not matrix: + continue + + new_list.extend(reversed(matrix.pop())) + if not matrix: + continue + + for i in range(len(matrix) - 1, -1, -1): + new_list.append(matrix[i].pop(0)) + if not matrix: + break + return new_list + + +MATRIX = [ + [1, 2, 3, 4, 5], + [16, 17, 18, 19, 6], + [15, 24, 25, 20, 7], + [14, 23, 22, 21, 8], + [13, 12, 11, 10, 9], +] + +print(snail_sort(MATRIX)) -- GitLab