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

spiral-matrix: implement. #526

Closed
wants to merge 6 commits 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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,18 @@
"text formatting"
]
},
{
"uuid": "666fda78-851e-4bb0-a416-953528f46bd7",
"slug": "spiral-matrix",
"core": false,
"unlocked_by": null,
"difficulty": 2,
"topics": [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add some topics of your choosing? You can use this list - exercism/problem-specifications#884 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure what to put. some idea ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Average-user I guess the following ones will be Ok:

loops
lists

algorithms is also suitable

"algorithms",
"control-flow (loops)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

control-flow (loops) should just be loops - see TOPICS.txt for a full list of valid topics.

"lists"
]
},
{
"uuid": "ecc97fc6-2e72-4325-9b67-b56c83b13a91",
"slug": "grep",
Expand Down
32 changes: 32 additions & 0 deletions exercises/spiral-matrix/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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
```
## Source
Copy link
Contributor

@N-Parsons N-Parsons Oct 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a blank line between sections, and there should actually be a ## Submitting exercises section here - check another exercise for the wording, or regenerate this README using the configlet.

To use the configlet (on Linux/OSX, not sure about commands for Windows):

$ cd exercism/python    # or wherever you have this repo cloned to
$ bin/fetch_configlet
$ bin/configlet generate . --only spiral-matrix


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.
11 changes: 11 additions & 0 deletions exercises/spiral-matrix/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def spiral(size):
m = [[0] * size for i in range(size)]
dx, dy = [0, 1, 0, -1], [1, 0, -1, 0]
x, y, c = 0, -1, 1
for k in range(2*size - 1):
for j in range((2*size - k) // 2):
x += dx[k % 4]
y += dy[k % 4]
m[x][y] = c
c += 1
return m
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 spiral(size):
pass
40 changes: 40 additions & 0 deletions exercises/spiral-matrix/spiral_matrix_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest

from spiral_matrix import spiral

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Tests are based on the version 1.0.0 of:
# https://github.com/exercism/problem-specifications/blob/master/exercises/spiral-matrix/canonical-data.json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be # Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 as per #784. Could you also move it down by a line? (ie. two blank lines before, one after)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want me to put something between those two slashes, or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Average-user, no, it's just a shorthand way of saying that we are skipping some uninteresting directories in the middle. The full path would be problem-specifications/exercises/spiral-matrix/canonical-data.json, which is a bit long to include in a comment, and the middle directories are fairly obvious.



class SpiralMatrixTest(unittest.TestCase):
def test_spiral_matrix_of_0(self):
self.assertEqual(spiral(0), [])

def test_spiral_matrix_of_1(self):
self.assertEqual(spiral(1), [[1]])

def test_spiral_matrix_of_2(self):
self.assertEqual(spiral(2), [[1, 2],
[4, 3]])

def test_spiral_matrix_of_3(self):
self.assertEqual(spiral(3), [[1, 2, 3],
[8, 9, 4],
[7, 6, 5]])

def test_spiral_matrix_of_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_of_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()