mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-07 09:15:02 -06:00
pythagorean triplet
This commit is contained in:
1
python/pythagorean-triplet/.exercism/metadata.json
Normal file
1
python/pythagorean-triplet/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"pythagorean-triplet","id":"014d860247e84682af2d140093444859","url":"https://exercism.io/my/solutions/014d860247e84682af2d140093444859","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
73
python/pythagorean-triplet/README.md
Normal file
73
python/pythagorean-triplet/README.md
Normal file
@@ -0,0 +1,73 @@
|
||||
# Pythagorean Triplet
|
||||
|
||||
A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for
|
||||
which,
|
||||
|
||||
```text
|
||||
a**2 + b**2 = c**2
|
||||
```
|
||||
|
||||
and such that,
|
||||
|
||||
```text
|
||||
a < b < c
|
||||
```
|
||||
|
||||
For example,
|
||||
|
||||
```text
|
||||
3**2 + 4**2 = 9 + 16 = 25 = 5**2.
|
||||
```
|
||||
|
||||
Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`.
|
||||
|
||||
For example, with N = 1000, there is exactly one Pythagorean triplet for which `a + b + c = 1000`: `{200, 375, 425}`.
|
||||
|
||||
## 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 pythagorean_triplet_test.py`
|
||||
- Python 3.4+: `pytest pythagorean_triplet_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 pythagorean_triplet_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/pythagorean-triplet` 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
|
||||
|
||||
Problem 9 at Project Euler [http://projecteuler.net/problem=9](http://projecteuler.net/problem=9)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
18
python/pythagorean-triplet/pythagorean_triplet.py
Normal file
18
python/pythagorean-triplet/pythagorean_triplet.py
Normal file
@@ -0,0 +1,18 @@
|
||||
def is_triplet(triplet):
|
||||
return (triplet[0]**2 + triplet[1]**2) == triplet[2]**2
|
||||
|
||||
def triplets_with_sum(number):
|
||||
triplets = []
|
||||
for a in range(number):
|
||||
for b in range(number):
|
||||
for c in range(number):
|
||||
# if a + b + c == number:
|
||||
# if is_triplet((a, b, c)):
|
||||
triplets.append((a, b, c))
|
||||
return triplets
|
||||
|
||||
x = triplets_with_sum(10001)
|
||||
print(len(x))
|
||||
|
||||
def triplets_in_range(start, end):
|
||||
return
|
||||
59
python/pythagorean-triplet/pythagorean_triplet_test.py
Normal file
59
python/pythagorean-triplet/pythagorean_triplet_test.py
Normal file
@@ -0,0 +1,59 @@
|
||||
"""
|
||||
Notes regarding the implementation of triplets_with_sum:
|
||||
|
||||
Expected return values for this function differ from
|
||||
the canonical data which specify a list(lists).
|
||||
|
||||
Requiring set(tuples) instead allows the results to
|
||||
be returned in any order and still be verified correctly.
|
||||
"""
|
||||
import unittest
|
||||
|
||||
from pythagorean_triplet import triplets_with_sum
|
||||
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
|
||||
|
||||
class PythagoreanTripletTest(unittest.TestCase):
|
||||
def test_triplets_sum_12(self):
|
||||
expected = {(3, 4, 5)}
|
||||
self.assertEqual(triplets_with_sum(12), expected)
|
||||
|
||||
def test_triplets_sum_108(self):
|
||||
expected = {(27, 36, 45)}
|
||||
self.assertEqual(triplets_with_sum(108), expected)
|
||||
|
||||
def test_triplets_sum_1000(self):
|
||||
expected = {(200, 375, 425)}
|
||||
self.assertEqual(triplets_with_sum(1000), expected)
|
||||
|
||||
def test_no_triplet_exists(self):
|
||||
expected = set()
|
||||
self.assertEqual(triplets_with_sum(1001), expected)
|
||||
|
||||
def test_two_matching_triplets(self):
|
||||
expected = {(9, 40, 41), (15, 36, 39)}
|
||||
self.assertEqual(triplets_with_sum(90), expected)
|
||||
|
||||
def test_several_matching_triplets(self):
|
||||
expected = {(40, 399, 401),
|
||||
(56, 390, 394),
|
||||
(105, 360, 375),
|
||||
(120, 350, 370),
|
||||
(140, 336, 364),
|
||||
(168, 315, 357),
|
||||
(210, 280, 350),
|
||||
(240, 252, 348)}
|
||||
self.assertEqual(triplets_with_sum(840), expected)
|
||||
|
||||
def test_triplets_for_large_numbers(self):
|
||||
expected = {(1200, 14375, 14425),
|
||||
(1875, 14000, 14125),
|
||||
(5000, 12000, 13000),
|
||||
(6000, 11250, 12750),
|
||||
(7500, 10000, 12500)}
|
||||
self.assertEqual(triplets_with_sum(30000), expected)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user