Skip to content

Commit db01de2

Browse files
chgraefcmccandless
authored andcommitted
Add spiral-matrix (exercism#1170)
* [WIP] Add spiral-matrix * Implement exercise spiral-matrix * Fix trailing whitespace and missing blank lines * Add exercise to config.json * Fix typo in config.json * Delete topic "logic" for spiral-matrix * Add reference to canonical data * Fix issue with missing blank line * Add blank line
1 parent e09fee4 commit db01de2

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

‎config.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,18 @@
12801280
"sets"
12811281
]
12821282
},
1283+
{
1284+
"uuid": "b0c7cf95-6470-4c1a-8eaa-6775310926a2",
1285+
"slug": "spiral-matrix",
1286+
"core": false,
1287+
"unlocked_by": null,
1288+
"difficulty": 2,
1289+
"topics": [
1290+
"algorithms",
1291+
"control-flow",
1292+
"lists"
1293+
]
1294+
},
12831295
{
12841296
"uuid": "e7351e8e-d3ff-4621-b818-cd55cf05bffd",
12851297
"slug": "accumulate",

‎exercises/spiral-matrix/README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Spiral Matrix
2+
3+
4+
Given the size, return a square matrix of numbers in spiral order.
5+
6+
The matrix should be filled with natural numbers, starting from 1
7+
in the top-left corner, increasing in an inward, clockwise spiral order,
8+
like these examples:
9+
10+
##### Spiral matrix of size 3
11+
12+
```plain
13+
1 2 3
14+
8 9 4
15+
7 6 5
16+
```
17+
18+
##### Spiral matrix of size 4
19+
20+
```plain
21+
1 2 3 4
22+
12 13 14 5
23+
11 16 15 6
24+
10 9 8 7
25+
```
26+
27+
## Submitting Exercises
28+
29+
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
30+
31+
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`.
32+
33+
For more detailed information about running tests, code style and linting,
34+
please see the [help page](http://exercism.io/languages/python).
35+
36+
## Source
37+
38+
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/)
39+
40+
## Submitting Incomplete Solutions
41+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
42+

‎exercises/spiral-matrix/example.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defspiral(size):
2+
sm= [[0]*sizeforkinrange(size)]
3+
i, j, el=0, -1, 1
4+
di, dj= [0, 1, 0, -1], [1, 0, -1, 0]
5+
forxinrange(2*size-1):
6+
foryinrange((2*size-x) //2):
7+
i+=di[x%4]
8+
j+=dj[x%4]
9+
sm[i][j] =el
10+
el+=1
11+
returnsm
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
defspiral(size):
2+
pass
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
importunittest
2+
3+
fromspiral_matriximportspiral
4+
5+
6+
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
7+
8+
9+
classSpiralMatrixTest(unittest.TestCase):
10+
deftest_spiral_matrix_with_size_0(self):
11+
self.assertEqual(spiral(0), [])
12+
13+
deftest_spiral_matrix_with_size_1(self):
14+
self.assertEqual(spiral(1), [[1]])
15+
16+
deftest_spiral_matrix_with_size_2(self):
17+
self.assertEqual(spiral(2), [[1, 2],
18+
[4, 3]])
19+
20+
deftest_spiral_matrix_with_size_3(self):
21+
self.assertEqual(spiral(3), [[1, 2, 3],
22+
[8, 9, 4],
23+
[7, 6, 5]])
24+
25+
deftest_spiral_matrix_with_size_4(self):
26+
self.assertEqual(spiral(4), [[1, 2, 3, 4],
27+
[12, 13, 14, 5],
28+
[11, 16, 15, 6],
29+
[10, 9, 8, 7]])
30+
31+
deftest_spiral_matrix_with_size_5(self):
32+
self.assertEqual(spiral(5), [[1, 2, 3, 4, 5],
33+
[16, 17, 18, 19, 6],
34+
[15, 24, 25, 20, 7],
35+
[14, 23, 22, 21, 8],
36+
[13, 12, 11, 10, 9]])
37+
38+
39+
if__name__=='__main__':
40+
unittest.main()

0 commit comments

Comments
(0)