spiral matrix and prime factors exercise

This commit is contained in:
Xevion
2019-07-24 14:37:17 -05:00
parent 307ae9d675
commit 5402cee2da
13 changed files with 549 additions and 9 deletions

View File

@@ -0,0 +1 @@
{"track":"python","exercise":"prime-factors","id":"8594f50d455a42c2bcaaf99a23da57ec","url":"https://exercism.io/my/solutions/8594f50d455a42c2bcaaf99a23da57ec","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,79 @@
# Prime Factors
Compute the prime factors of a given natural number.
A prime number is only evenly divisible by itself and 1.
Note that 1 is not a prime number.
## Example
What are the prime factors of 60?
- Our first divisor is 2. 2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
- 3 goes cleanly into 15, leaving 5.
- 3 does not go cleanly into 5. The next possible factor is 4.
- 4 does not go cleanly into 5. The next possible factor is 5.
- 5 does go cleanly into 5.
- We're left only with 1, so now, we're done.
Our successful divisors in that computation represent the list of prime
factors of 60: 2, 2, 3, and 5.
You can check this yourself:
- 2 * 2 * 3 * 5
- = 4 * 15
- = 60
- Success!
## 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 prime_factors_test.py`
- Python 3.4+: `pytest prime_factors_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 prime_factors_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/prime-factors` 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
The Prime Factors Kata by Uncle Bob [http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata](http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@@ -0,0 +1,8 @@
def factors(value):
factors, n = [], 2
while value > 1:
while value % n == 0:
factors.append(n)
value /= n
n += 1
return factors

View File

@@ -0,0 +1,32 @@
import unittest
from prime_factors import factors
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
class PrimeFactorsTest(unittest.TestCase):
def test_no_factors(self):
self.assertEqual(factors(1), [])
def test_prime_number(self):
self.assertEqual(factors(2), [2])
def test_square_of_a_prime(self):
self.assertEqual(factors(9), [3, 3])
def test_cube_of_a_prime(self):
self.assertEqual(factors(8), [2, 2, 2])
def test_product_of_primes_and_non_primes(self):
self.assertEqual(factors(12), [2, 2, 3])
def test_product_of_primes(self):
self.assertEqual(factors(901255), [5, 17, 23, 461])
def test_factors_include_a_large_prime(self):
self.assertEqual(factors(93819012551), [11, 9539, 894119])
if __name__ == '__main__':
unittest.main()