-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add spiral-matrix #1170
Merged
Merged
Add spiral-matrix #1170
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
450c10f
[WIP] Add spiral-matrix
chgraef b45e6d1
Merge branch 'master' of https://github.com/exercism/python into spir…
chgraef c296de5
Implement exercise spiral-matrix
chgraef fa9f146
Fix trailing whitespace and missing blank lines
chgraef c29a55a
Add exercise to config.json
chgraef 258030a
Fix typo in config.json
chgraef 2cdc887
Delete topic "logic" for spiral-matrix
chgraef cb946fa
Add reference to canonical data
chgraef 391247d
Merge branch 'master' into spiral-matrix
chgraef 88e1c7f
Fix issue with missing blank line
chgraef e443ff3
Merge branch 'spiral-matrix' of github.com:chgraef/python into spiral…
chgraef 64295ab
Add blank line
chgraef cb24d72
Merge branch 'master' into spiral-matrix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/<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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
def spiral(size): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import unittest | ||
|
||
from spiral_matrix import spiral | ||
|
||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0 | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment referencing the current version of the canonical data. For the format of this comment, please see #784. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks for the hint. |
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move this line down by one to be more consistent with the other test files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @N-Parsons ,will do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe for full consistency, you the spacing should be 2 blank lines above, one below (see below):