Skip to content

Commit

Permalink
implement exercise spiral-matrix closes exercism#752
Browse files Browse the repository at this point in the history
  • Loading branch information
nqeron committed Oct 29, 2017
1 parent 9f25653 commit df0ab53
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
40 changes: 40 additions & 0 deletions exercises/spiral-matrix/README.md
Original file line number Diff line number Diff line change
@@ -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/<exerciseName>` directory.

For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/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.
2 changes: 2 additions & 0 deletions exercises/spiral-matrix/spiral_matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def matrix(num):
pass
43 changes: 43 additions & 0 deletions exercises/spiral-matrix/spiral_matrix_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit df0ab53

Please sign in to comment.