atbash, robot simulator, sum of multiples exercises

This commit is contained in:
Xevion
2019-07-17 12:22:57 -05:00
parent 811357b6f8
commit 21a1f6cf69
12 changed files with 512 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"python","exercise":"sum-of-multiples","id":"376b2f4ace694adf9de021c6cc10e243","url":"https://exercism.io/my/solutions/376b2f4ace694adf9de021c6cc10e243","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,58 @@
# Sum Of Multiples
Given a number, find the sum of all the unique multiples of particular numbers up to
but not including that number.
If we list all the natural numbers below 20 that are multiples of 3 or 5,
we get 3, 5, 6, 9, 10, 12, 15, and 18.
The sum of these multiples is 78.
## 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 sum_of_multiples_test.py`
- Python 3.4+: `pytest sum_of_multiples_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 sum_of_multiples_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/sum-of-multiples` 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
A variation on Problem 1 at Project Euler [http://projecteuler.net/problem=1](http://projecteuler.net/problem=1)
## 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,5 @@
ismultiple = lambda num, multiples: any(num % sub == 0 for sub in multiples)
def sum_of_multiples(limit, multiples):
multiples = [mult for mult in multiples if mult != 0]
return sum([i for i in range(1, limit) if ismultiple(i, multiples)])

View File

@@ -0,0 +1,69 @@
"""
You can make the following assumptions about the inputs to the
'sum_of_multiples' function:
* All input numbers are non-negative 'int's, i.e. natural numbers
including zero.
* A list of factors must be given, and its elements are unique
and sorted in ascending order.
"""
import unittest
from sum_of_multiples import sum_of_multiples
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.5.0
class SumOfMultiplesTest(unittest.TestCase):
def test_multiples_with_no_factors_in_limit(self):
self.assertEqual(sum_of_multiples(1, [3, 5]), 0)
def test_multiples_of_one_factor_within_limit(self):
self.assertEqual(sum_of_multiples(4, [3, 5]), 3)
def test_various_multiples_in_limit(self):
self.assertEqual(sum_of_multiples(7, [3]), 9)
def test_various_factors_with_multiples_in_limit(self):
self.assertEqual(sum_of_multiples(10, [3, 5]), 23)
def test_multiples_counted_only_once(self):
self.assertEqual(sum_of_multiples(100, [3, 5]), 2318)
def test_multiples_with_large_limit(self):
self.assertEqual(sum_of_multiples(1000, [3, 5]), 233168)
def test_multiples_with_three_factors(self):
self.assertEqual(sum_of_multiples(20, [7, 13, 17]), 51)
def test_multiples_with_factors_not_prime(self):
self.assertEqual(sum_of_multiples(15, [4, 6]), 30)
def test_multiples_with_factors_prime_and_not(self):
self.assertEqual(sum_of_multiples(150, [5, 6, 8]), 4419)
def test_multiples_with_similar_factors(self):
self.assertEqual(sum_of_multiples(51, [5, 25]), 275)
def test_multiples_with_large_factors(self):
self.assertEqual(sum_of_multiples(10000, [43, 47]), 2203160)
def test_multiples_of_one_will_be_all(self):
self.assertEqual(sum_of_multiples(100, [1]), 4950)
def test_multiples_of_an_empty_list(self):
self.assertEqual(sum_of_multiples(10000, []), 0)
def test_multiples_of_zero_will_be_none(self):
self.assertEqual(sum_of_multiples(1, [0]), 0)
def test_multiples_with_a_zero_factor(self):
self.assertEqual(sum_of_multiples(4, [0, 3]), 3)
def test_multiples_of_several_factors(self):
self.assertEqual(sum_of_multiples(10000,
[2, 3, 5, 7, 11]), 39614537)
if __name__ == '__main__':
unittest.main()