diff --git a/config.json b/config.json index 10fd138251..d14965fa3c 100644 --- a/config.json +++ b/config.json @@ -1280,6 +1280,18 @@ "sets" ] }, + { + "uuid": "b0c7cf95-6470-4c1a-8eaa-6775310926a2", + "slug": "spiral-matrix", + "core": false, + "unlocked_by": null, + "difficulty": 2, + "topics": [ + "algorithms", + "control-flow", + "lists" + ] + }, { "uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd", "slug": "accumulate", 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..9bd6e485bf --- /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..a164035d42 --- /dev/null +++ b/exercises/spiral-matrix/spiral_matrix_test.py @@ -0,0 +1,40 @@ +import unittest + +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): + 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()