mirror of
https://github.com/Xevion/exercism.git
synced 2026-01-31 06:24:15 -06:00
spiral matrix and prime factors exercise
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"spiral-matrix","id":"e351192377fc41329076c9d6636ef233","url":"https://exercism.io/my/solutions/e351192377fc41329076c9d6636ef233","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
@@ -0,0 +1,73 @@
|
||||
# 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
|
||||
```
|
||||
|
||||
## Exception messages
|
||||
|
||||
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
|
||||
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
|
||||
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
|
||||
a message.
|
||||
|
||||
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
|
||||
`raise Exception`, you should write:
|
||||
|
||||
```python
|
||||
raise Exception("Meaningful message indicating the source of the error")
|
||||
```
|
||||
|
||||
## Running the tests
|
||||
|
||||
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
|
||||
|
||||
- Python 2.7: `py.test spiral_matrix_test.py`
|
||||
- Python 3.4+: `pytest spiral_matrix_test.py`
|
||||
|
||||
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
|
||||
`python -m pytest spiral_matrix_test.py`
|
||||
|
||||
### Common `pytest` options
|
||||
|
||||
- `-v` : enable verbose output
|
||||
- `-x` : stop running tests on first failure
|
||||
- `--ff` : run failures from previous test before running other test cases
|
||||
|
||||
For other options, see `python -m pytest -h`
|
||||
|
||||
## Submitting Exercises
|
||||
|
||||
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/spiral-matrix` directory.
|
||||
|
||||
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
|
||||
|
||||
For more detailed information about running tests, code style and linting,
|
||||
please see [Running the Tests](http://exercism.io/tracks/python/tests).
|
||||
|
||||
## 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.
|
||||
@@ -0,0 +1,53 @@
|
||||
# Two lines, dude. ez.
|
||||
def spiral_matrix(size):
|
||||
return [] if size < 1 else Matrix(size).matrix
|
||||
|
||||
# Class for a pathfinding based spiral generation
|
||||
class Matrix:
|
||||
def __init__(self, size):
|
||||
self.size = size
|
||||
self.matrix = [[None for y in range(size)] for x in range(size)]
|
||||
self.i = 1
|
||||
self.cur = (0, 0)
|
||||
self.cardinals = [(0, 1), (1, 0), (-1, 0), (0, -1)]
|
||||
self.dir_index = 0
|
||||
self.loop()
|
||||
|
||||
# Loop that builds the spiral matrix
|
||||
def loop(self):
|
||||
# While the current number is less than the maximum number
|
||||
while self.i < (self.size ** 2):
|
||||
# If the next position is not valid, turn
|
||||
if not self.valid(self.nextpos):
|
||||
self.dir_index = (self.dir_index + 1) % 4
|
||||
else:
|
||||
self.access()
|
||||
self.cur = self.nextpos
|
||||
self.access()
|
||||
|
||||
# Access a position and increment the counter
|
||||
def access(self):
|
||||
self.matrix[self.cur[0]][self.cur[1]] = self.i
|
||||
self.i += 1
|
||||
|
||||
# Just the current direction (as an offset)
|
||||
@property
|
||||
def direction(self):
|
||||
return self.cardinals[self.dir_index]
|
||||
|
||||
# Next position for access based on the current direction
|
||||
@property
|
||||
def nextpos(self):
|
||||
return (self.cur[0] + self.direction[0], self.cur[1] + self.direction[1])
|
||||
|
||||
# Determine whether a position is valid to be approached
|
||||
def valid(self, pos):
|
||||
return self.validxy(pos[0], pos[1]) and not self.matrix[pos[0]][pos[1]]
|
||||
|
||||
# Determine whether a position is
|
||||
def validxy(self, x, y):
|
||||
return x >= 0 and x < self.size and y >= 0 and y < self.size
|
||||
|
||||
# Printable Matrix with proper character space justification
|
||||
def __repr__(self):
|
||||
return '\n'.join([' '.join(map(lambda item : str(item or '?').rjust(len(str(self.size ** 2))), sub)) for sub in self.matrix])
|
||||
@@ -0,0 +1,51 @@
|
||||
import unittest
|
||||
|
||||
from spiral_matrix import spiral_matrix
|
||||
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
|
||||
|
||||
|
||||
class SpiralMatrixTest(unittest.TestCase):
|
||||
def test_empty_spiral(self):
|
||||
self.assertEqual(spiral_matrix(0), [
|
||||
])
|
||||
|
||||
def test_trivial_spiral(self):
|
||||
self.assertEqual(spiral_matrix(1), [
|
||||
[1]
|
||||
])
|
||||
|
||||
def test_spiral_of_size_2(self):
|
||||
self.assertEqual(spiral_matrix(2), [
|
||||
[1, 2],
|
||||
[4, 3]
|
||||
])
|
||||
|
||||
def test_spiral_of_size_3(self):
|
||||
self.assertEqual(spiral_matrix(3), [
|
||||
[1, 2, 3],
|
||||
[8, 9, 4],
|
||||
[7, 6, 5]
|
||||
])
|
||||
|
||||
def test_spiral_of_size_4(self):
|
||||
self.assertEqual(spiral_matrix(4), [
|
||||
[1, 2, 3, 4],
|
||||
[12, 13, 14, 5],
|
||||
[11, 16, 15, 6],
|
||||
[10, 9, 8, 7]
|
||||
])
|
||||
|
||||
def test_spiral_of_size_5(self):
|
||||
self.assertEqual(spiral_matrix(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()
|
||||
Reference in New Issue
Block a user