Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement exercise spiral-matrix closes #752 #1074

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()