From df0ab53e1b66903638a989618b72cc3585c1ced5 Mon Sep 17 00:00:00 2001 From: nqeron Date: Sun, 29 Oct 2017 12:57:22 -0400 Subject: [PATCH] implement exercise spiral-matrix closes #752 --- exercises/spiral-matrix/README.md | 40 +++++++++++++++++ exercises/spiral-matrix/spiral_matrix.py | 2 + exercises/spiral-matrix/spiral_matrix_test.py | 43 +++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 exercises/spiral-matrix/README.md 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..bb07f6b15d --- /dev/null +++ b/exercises/spiral-matrix/README.md @@ -0,0 +1,40 @@ +# 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 + +```text +1 2 3 +8 9 4 +7 6 5 +``` + +###### Spiral matrix of size 4 + +```text + 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/spiral_matrix.py b/exercises/spiral-matrix/spiral_matrix.py new file mode 100644 index 0000000000..c76a340935 --- /dev/null +++ b/exercises/spiral-matrix/spiral_matrix.py @@ -0,0 +1,2 @@ +def matrix(num): + 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..d74e9a4369 --- /dev/null +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -0,0 +1,43 @@ +import unittest + +from spiral_matrix import matrix + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 + +class SpiralMatrixTests(unittest.TestCase): + def test_empty_spiral(self): + expected = [] + self.assertEqual(matrix(0),expected) + + def test_trivial_spiral(self): + expected = [[1]] + self.assertEqual(matrix(1),expected) + + def test_spiral_size_2(self): + expected = [[1,2], + [4,3]] + self.assertEqual(matrix(2),expected) + + def test_spiral_size_3(self): + expected = [[1,2,3], + [8,9,4], + [7,6,5]] + self.assertEqual(matrix(3),expected) + + def test_spiral_size_4(self): + expected = [[1,2,3,4], + [12,13,14,5], + [11,16,15,6], + [10, 9, 8,7]] + self.assertEqual(matrix(4),expected) + + def test_spiral_size_5(self): + expected = [[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]] + self.assertEqual(matrix(5),expected) + +if __name__ == '__main__': + unittest.main()