From 450c10feea56387ee1a0b8db3e698292aaa7f616 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Fri, 19 Jan 2018 21:49:00 +0100 Subject: [PATCH 1/9] [WIP] Add spiral-matrix From c296de52a5323de36833d3cf72ed8496b00f9123 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Sun, 21 Jan 2018 17:55:41 +0100 Subject: [PATCH 2/9] Implement exercise spiral-matrix --- exercises/spiral-matrix/README.md | 42 +++++++++++++++++++ exercises/spiral-matrix/example.py | 11 +++++ exercises/spiral-matrix/spiral_matrix.py | 2 + exercises/spiral-matrix/spiral_matrix_test.py | 35 ++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 exercises/spiral-matrix/README.md create mode 100644 exercises/spiral-matrix/example.py create mode 100644 exercises/spiral-matrix/spiral_matrix.py create mode 100644 exercises/spiral-matrix/spiral_matrix_test.py diff --git a/exercises/spiral-matrix/README.md b/exercises/spiral-matrix/README.md new file mode 100644 index 0000000000..d68987b21c --- /dev/null +++ b/exercises/spiral-matrix/README.md @@ -0,0 +1,42 @@ +# Spiral Matrix + + +Given the size, return a square matrix of numbers in spiral order. + +The matrix should be filled with natural numbers, starting from 1 +in the top-left corner, increasing in an inward, clockwise spiral order, +like these examples: + +##### Spiral matrix of size 3 + +```plain +1 2 3 +8 9 4 +7 6 5 +``` + +##### Spiral matrix of size 4 + +```plain + 1 2 3 4 +12 13 14 5 +11 16 15 6 +10 9 8 7 +``` + +## Submitting Exercises + +Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/` directory. + +For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit /python/bob/bob.py`. + +For more detailed information about running tests, code style and linting, +please see the [help page](http://exercism.io/languages/python). + +## Source + +Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. [https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/](https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + diff --git a/exercises/spiral-matrix/example.py b/exercises/spiral-matrix/example.py new file mode 100644 index 0000000000..899de68499 --- /dev/null +++ b/exercises/spiral-matrix/example.py @@ -0,0 +1,11 @@ +def spiral(size): + sm = [[0]*size for k in range(size)] + i, j, el = 0, -1, 1 + di, dj = [0, 1, 0, -1], [1, 0, -1, 0] + for x in range(2*size - 1): + for y in range((2*size - x) // 2): + i += di[x % 4] + j += dj[x % 4] + sm[i][j] = el + el += 1 + return sm diff --git a/exercises/spiral-matrix/spiral_matrix.py b/exercises/spiral-matrix/spiral_matrix.py new file mode 100644 index 0000000000..5eaa311cb8 --- /dev/null +++ b/exercises/spiral-matrix/spiral_matrix.py @@ -0,0 +1,2 @@ +def spiral(size): + pass diff --git a/exercises/spiral-matrix/spiral_matrix_test.py b/exercises/spiral-matrix/spiral_matrix_test.py new file mode 100644 index 0000000000..959ba5eb79 --- /dev/null +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -0,0 +1,35 @@ +import unittest + +from spiral_matrix import spiral + +class SpiralMatrixTest(unittest.TestCase): + def test_spiral_matrix_with_size_0(self): + self.assertEqual(spiral(0), []) + + def test_spiral_matrix_with_size_1(self): + self.assertEqual(spiral(1), [[1]]) + + def test_spiral_matrix_with_size_2(self): + self.assertEqual(spiral(2), [[1, 2], + [4, 3]]) + + def test_spiral_matrix_with_size_3(self): + self.assertEqual(spiral(3), [[1, 2, 3], + [8, 9, 4], + [7, 6, 5]]) + + def test_spiral_matrix_with_size_4(self): + self.assertEqual(spiral(4), [[1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7]]) + + def test_spiral_matrix_with_size_5(self): + self.assertEqual(spiral(5), [[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]]) + +if __name__ == '__main__': + unittest.main() From fa9f1462d8e2ef7088afc5e0891282d1264f77c2 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Sun, 21 Jan 2018 18:06:53 +0100 Subject: [PATCH 3/9] Fix trailing whitespace and missing blank lines --- exercises/spiral-matrix/example.py | 2 +- exercises/spiral-matrix/spiral_matrix_test.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/exercises/spiral-matrix/example.py b/exercises/spiral-matrix/example.py index 899de68499..9bd6e485bf 100644 --- a/exercises/spiral-matrix/example.py +++ b/exercises/spiral-matrix/example.py @@ -8,4 +8,4 @@ def spiral(size): j += dj[x % 4] sm[i][j] = el el += 1 - return sm + return sm diff --git a/exercises/spiral-matrix/spiral_matrix_test.py b/exercises/spiral-matrix/spiral_matrix_test.py index 959ba5eb79..e62937ef4b 100644 --- a/exercises/spiral-matrix/spiral_matrix_test.py +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -2,6 +2,7 @@ from spiral_matrix import spiral + class SpiralMatrixTest(unittest.TestCase): def test_spiral_matrix_with_size_0(self): self.assertEqual(spiral(0), []) @@ -31,5 +32,6 @@ def test_spiral_matrix_with_size_5(self): [14, 23, 22, 21, 8], [13, 12, 11, 10, 9]]) + if __name__ == '__main__': unittest.main() From c29a55a27d48af2223349c41c0bb4854433074a7 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Sun, 21 Jan 2018 18:16:47 +0100 Subject: [PATCH 4/9] Add exercise to config.json --- config.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config.json b/config.json index edfb8dad84..52067440a6 100644 --- a/config.json +++ b/config.json @@ -1268,6 +1268,19 @@ "sets" ] }, + { + "uuid": "b0c7cf95-6470-4c1a-8eaa-6775310926a2", + "slug": "spiral-matrix", + "core": "false", + "unlocked_by": null, + "difficulty": 2, + "topics": [ + "algorithms", + "control-flow", + "lists", + "logic" + ] + }, { "uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd", "slug": "accumulate", From 258030a2b30313dfbde757e9416e89776193898f Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Sun, 21 Jan 2018 18:20:31 +0100 Subject: [PATCH 5/9] Fix typo in config.json --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index 52067440a6..f5103cea4e 100644 --- a/config.json +++ b/config.json @@ -1271,7 +1271,7 @@ { "uuid": "b0c7cf95-6470-4c1a-8eaa-6775310926a2", "slug": "spiral-matrix", - "core": "false", + "core": false, "unlocked_by": null, "difficulty": 2, "topics": [ From 2cdc887d7dfded10d96e998b027fa5a3c4c3984a Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Tue, 23 Jan 2018 19:14:30 +0100 Subject: [PATCH 6/9] Delete topic "logic" for spiral-matrix --- config.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/config.json b/config.json index f5103cea4e..d58a62bd75 100644 --- a/config.json +++ b/config.json @@ -1277,8 +1277,7 @@ "topics": [ "algorithms", "control-flow", - "lists", - "logic" + "lists" ] }, { From cb946fa57fcd4cdd6c4cf883ec45b677bcdd0d63 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Tue, 23 Jan 2018 19:16:57 +0100 Subject: [PATCH 7/9] Add reference to canonical data --- exercises/spiral-matrix/spiral_matrix_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/spiral-matrix/spiral_matrix_test.py b/exercises/spiral-matrix/spiral_matrix_test.py index e62937ef4b..f5ab0b6547 100644 --- a/exercises/spiral-matrix/spiral_matrix_test.py +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -2,6 +2,7 @@ from spiral_matrix import spiral +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0 class SpiralMatrixTest(unittest.TestCase): def test_spiral_matrix_with_size_0(self): From 88e1c7f6e3cd2271ec2c0a4577aa9d3d148aa415 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Tue, 23 Jan 2018 19:21:30 +0100 Subject: [PATCH 8/9] Fix issue with missing blank line --- exercises/spiral-matrix/spiral_matrix_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/spiral-matrix/spiral_matrix_test.py b/exercises/spiral-matrix/spiral_matrix_test.py index f5ab0b6547..e009fe40ab 100644 --- a/exercises/spiral-matrix/spiral_matrix_test.py +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -4,6 +4,7 @@ # Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0 + class SpiralMatrixTest(unittest.TestCase): def test_spiral_matrix_with_size_0(self): self.assertEqual(spiral(0), []) From 64295ab187b96ba1ffa419cf05f18cc4d7665d63 Mon Sep 17 00:00:00 2001 From: chgrae <1595191+chgraef@users.noreply.github.com> Date: Fri, 26 Jan 2018 11:14:56 +0100 Subject: [PATCH 9/9] Add blank line --- exercises/spiral-matrix/spiral_matrix_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/exercises/spiral-matrix/spiral_matrix_test.py b/exercises/spiral-matrix/spiral_matrix_test.py index e009fe40ab..a164035d42 100644 --- a/exercises/spiral-matrix/spiral_matrix_test.py +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -2,6 +2,7 @@ from spiral_matrix import spiral + # Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0