mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-06 01:14:56 -06:00
flatten and space age exercise
This commit is contained in:
1
python/flatten-array/.exercism/metadata.json
Normal file
1
python/flatten-array/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"flatten-array","id":"d6dcaaa2dc0f4276ab4255f47dfb570a","url":"https://exercism.io/my/solutions/d6dcaaa2dc0f4276ab4255f47dfb570a","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
60
python/flatten-array/README.md
Normal file
60
python/flatten-array/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# Flatten Array
|
||||
|
||||
Take a nested list and return a single flattened list with all values except nil/null.
|
||||
|
||||
The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.
|
||||
|
||||
For Example
|
||||
|
||||
input: [1,[2,3,null,4],[null],5]
|
||||
|
||||
output: [1,2,3,4,5]
|
||||
|
||||
## 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 flatten_array_test.py`
|
||||
- Python 3.4+: `pytest flatten_array_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 flatten_array_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/flatten-array` 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
|
||||
|
||||
Interview Question [https://reference.wolfram.com/language/ref/Flatten.html](https://reference.wolfram.com/language/ref/Flatten.html)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
3
python/flatten-array/flatten_array.py
Normal file
3
python/flatten-array/flatten_array.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from functools import partial
|
||||
flatten_base = lambda l : [l] if type(l) != list else sum(map(flatten_base, l), [])
|
||||
flatten = lambda l : list(filter(lambda item : item is not None, flatten_base(l)))
|
||||
47
python/flatten-array/flatten_array_test.py
Normal file
47
python/flatten-array/flatten_array_test.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import unittest
|
||||
|
||||
from flatten_array import flatten
|
||||
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
|
||||
|
||||
class FlattenArrayTest(unittest.TestCase):
|
||||
|
||||
def test_no_nesting(self):
|
||||
self.assertEqual(flatten([0, 1, 2]), [0, 1, 2])
|
||||
|
||||
def test_flatten_integers(self):
|
||||
inputs = [1, [2, 3, 4, 5, 6, 7], 8]
|
||||
expected = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
self.assertEqual(flatten(inputs), expected)
|
||||
|
||||
def test_five_level_nesting(self):
|
||||
inputs = [0, 2, [[2, 3], 8, 100, 4, [[[50]]]], -2]
|
||||
expected = [0, 2, 2, 3, 8, 100, 4, 50, -2]
|
||||
self.assertEqual(flatten(inputs), expected)
|
||||
|
||||
def test_six_level_nesting(self):
|
||||
inputs = [1, [2, [[3]], [4, [[5]]], 6, 7], 8]
|
||||
expected = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
self.assertEqual(flatten(inputs), expected)
|
||||
|
||||
def test_with_none_values(self):
|
||||
inputs = [0, 2, [[2, 3], 8, [[100]], None, [[None]]], -2]
|
||||
expected = [0, 2, 2, 3, 8, 100, -2]
|
||||
self.assertEqual(flatten(inputs), expected)
|
||||
|
||||
def test_all_values_are_none(self):
|
||||
inputs = [None, [[[None]]], None, None, [[None, None], None], None]
|
||||
expected = []
|
||||
self.assertEqual(flatten(inputs), expected)
|
||||
|
||||
# Additional tests for this track
|
||||
def test_empty_nested_lists(self):
|
||||
self.assertEqual(flatten([[()]]), [])
|
||||
|
||||
def test_strings(self):
|
||||
self.assertEqual(flatten(['0', ['1', '2']]), ['0', '1', '2'])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
1
python/space-age/.exercism/metadata.json
Normal file
1
python/space-age/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"python","exercise":"space-age","id":"22ab7b841514483ca523bc4c9995969d","url":"https://exercism.io/my/solutions/22ab7b841514483ca523bc4c9995969d","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||
67
python/space-age/README.md
Normal file
67
python/space-age/README.md
Normal file
@@ -0,0 +1,67 @@
|
||||
# Space Age
|
||||
|
||||
Given an age in seconds, calculate how old someone would be on:
|
||||
|
||||
- Earth: orbital period 365.25 Earth days, or 31557600 seconds
|
||||
- Mercury: orbital period 0.2408467 Earth years
|
||||
- Venus: orbital period 0.61519726 Earth years
|
||||
- Mars: orbital period 1.8808158 Earth years
|
||||
- Jupiter: orbital period 11.862615 Earth years
|
||||
- Saturn: orbital period 29.447498 Earth years
|
||||
- Uranus: orbital period 84.016846 Earth years
|
||||
- Neptune: orbital period 164.79132 Earth years
|
||||
|
||||
So if you were told someone were 1,000,000,000 seconds old, you should
|
||||
be able to say that they're 31.69 Earth-years old.
|
||||
|
||||
If you're wondering why Pluto didn't make the cut, go watch [this
|
||||
youtube video](http://www.youtube.com/watch?v=Z_2gbGXzFbs).
|
||||
|
||||
## 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 space_age_test.py`
|
||||
- Python 3.4+: `pytest space_age_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 space_age_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/space-age` 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
|
||||
|
||||
Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=01](http://pine.fm/LearnToProgram/?Chapter=01)
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
17
python/space-age/space_age.py
Normal file
17
python/space-age/space_age.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from functools import partial
|
||||
|
||||
class SpaceAge(object):
|
||||
def __init__(self, seconds):
|
||||
self.seconds = seconds
|
||||
self.gen = lambda n : partial(self.factor, n)
|
||||
self.on_mercury = self.gen(0.241)
|
||||
self.on_venus = self.gen(0.615)
|
||||
self.on_earth = self.gen(1.00070137)
|
||||
self.on_mars = self.gen(1.881)
|
||||
self.on_jupiter = self.gen(11.862)
|
||||
self.on_saturn = self.gen(29.457)
|
||||
self.on_uranus = self.gen(84.011)
|
||||
self.on_neptune = self.gen(164.79)
|
||||
|
||||
def factor(self, n):
|
||||
return round(self.seconds / (n * 365 * 24 * 60 * 60), 2)
|
||||
41
python/space-age/space_age_test.py
Normal file
41
python/space-age/space_age_test.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import unittest
|
||||
|
||||
from space_age import SpaceAge
|
||||
|
||||
|
||||
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
|
||||
|
||||
class SpaceAgeTest(unittest.TestCase):
|
||||
|
||||
def test_age_on_mercury(self):
|
||||
self.assertEqual(SpaceAge(2134835688).on_mercury(), 280.88)
|
||||
|
||||
def test_age_on_venus(self):
|
||||
self.assertEqual(SpaceAge(189839836).on_venus(), 9.78)
|
||||
|
||||
def test_age_on_earth(self):
|
||||
self.assertEqual(SpaceAge(1000000000).on_earth(), 31.69)
|
||||
|
||||
def test_age_on_mars(self):
|
||||
self.assertEqual(SpaceAge(2129871239).on_mars(), 35.88)
|
||||
|
||||
def test_age_on_jupiter(self):
|
||||
self.assertEqual(SpaceAge(901876382).on_jupiter(), 2.41)
|
||||
|
||||
def test_age_on_saturn(self):
|
||||
self.assertEqual(SpaceAge(2000000000).on_saturn(), 2.15)
|
||||
|
||||
def test_age_on_uranus(self):
|
||||
self.assertEqual(SpaceAge(1210123456).on_uranus(), 0.46)
|
||||
|
||||
def test_age_on_neptune(self):
|
||||
self.assertEqual(SpaceAge(1821023456).on_neptune(), 0.35)
|
||||
|
||||
# Additional tests for this track
|
||||
|
||||
def test_age_in_seconds(self):
|
||||
self.assertEqual(SpaceAge(1e6).seconds, 1e6)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user