mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-07 20:07:00 -06:00
sublist exercise
This commit is contained in:
1
python/sublist/.exercism/metadata.json
Normal file
1
python/sublist/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"python","exercise":"sublist","id":"a217429e9ed74c7f8d0e72bb6660d881","url":"https://exercism.io/my/solutions/a217429e9ed74c7f8d0e72bb6660d881","handle":"Xevion","is_requester":true,"auto_approve":false}
|
||||||
63
python/sublist/README.md
Normal file
63
python/sublist/README.md
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# Sublist
|
||||||
|
|
||||||
|
Given two lists determine if the first list is contained within the second
|
||||||
|
list, if the second list is contained within the first list, if both lists are
|
||||||
|
contained within each other or if none of these are true.
|
||||||
|
|
||||||
|
Specifically, a list A is a sublist of list B if by dropping 0 or more elements
|
||||||
|
from the front of B and 0 or more elements from the back of B you get a list
|
||||||
|
that's completely equal to A.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
* A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B
|
||||||
|
* A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B
|
||||||
|
* A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B
|
||||||
|
* A = [1, 2, 3], B = [1, 2, 3], A is equal to B
|
||||||
|
* A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B
|
||||||
|
* A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B
|
||||||
|
|
||||||
|
## 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 sublist_test.py`
|
||||||
|
- Python 3.4+: `pytest sublist_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 sublist_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/sublist` 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).
|
||||||
|
|
||||||
|
## Submitting Incomplete Solutions
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||||
35
python/sublist/sublist.py
Normal file
35
python/sublist/sublist.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
"""
|
||||||
|
This exercise stub and the test suite contain several enumerated constants.
|
||||||
|
|
||||||
|
Since Python 2 does not have the enum module, the idiomatic way to write
|
||||||
|
enumerated constants has traditionally been a NAME assigned to an arbitrary,
|
||||||
|
but unique value. An integer is traditionally used because it’s memory
|
||||||
|
efficient.
|
||||||
|
It is a common practice to export both constants and functions that work with
|
||||||
|
those constants (ex. the constants in the os, subprocess and re modules).
|
||||||
|
|
||||||
|
You can learn more here: https://en.wikipedia.org/wiki/Enumerated_type
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Possible sublist categories.
|
||||||
|
# Change the values as you see fit.
|
||||||
|
SUBLIST = 0
|
||||||
|
SUPERLIST = 1
|
||||||
|
EQUAL = 2
|
||||||
|
UNEQUAL = 3
|
||||||
|
|
||||||
|
def subfinder(mylist, pattern):
|
||||||
|
matches = []
|
||||||
|
for i, val in enumerate(mylist):
|
||||||
|
if val == pattern[0] and mylist[i:i+len(pattern)] == pattern:
|
||||||
|
matches.append(pattern)
|
||||||
|
return matches
|
||||||
|
|
||||||
|
def sublist(one, two):
|
||||||
|
if one == two: return EQUAL
|
||||||
|
if not one or not two: return SUBLIST if not one else SUPERLIST if not two else UNEQUAL
|
||||||
|
return SUPERLIST if subfinder(one, two) else SUBLIST if subfinder(two, one) else UNEQUAL
|
||||||
|
|
||||||
|
x, y = [1, 2, 4], [5, 6, 1, 1, 2, 4, 7, 8]
|
||||||
|
print(sublist(x, y))
|
||||||
|
print(sublist(y, x))
|
||||||
142
python/sublist/sublist_test.py
Normal file
142
python/sublist/sublist_test.py
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from sublist import sublist, SUBLIST, SUPERLIST, EQUAL, UNEQUAL
|
||||||
|
|
||||||
|
|
||||||
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
|
||||||
|
|
||||||
|
class SublistTest(unittest.TestCase):
|
||||||
|
def test_empty_lists(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([], []),
|
||||||
|
EQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_empty_list_within_non_empty_list(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([], [1, 2, 3]),
|
||||||
|
SUBLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_non_empty_list_contains_empty_list(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 2, 3], []),
|
||||||
|
SUPERLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_list_equals_itself(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 2, 3], [1, 2, 3]),
|
||||||
|
EQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_different_lists(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 2, 3], [2, 3, 4]),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_false_start(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 2, 5], [0, 1, 2, 3, 1, 2, 5, 6]),
|
||||||
|
SUBLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_consecutive(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 1, 2], [0, 1, 1, 1, 2, 1, 2]),
|
||||||
|
SUBLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_sublist_at_start(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([0, 1, 2], [0, 1, 2, 3, 4, 5]),
|
||||||
|
SUBLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_sublist_in_middle(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([2, 3, 4], [0, 1, 2, 3, 4, 5]),
|
||||||
|
SUBLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_sublist_at_end(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([3, 4, 5], [0, 1, 2, 3, 4, 5]),
|
||||||
|
SUBLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_at_start_of_superlist(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([0, 1, 2, 3, 4, 5], [0, 1, 2]),
|
||||||
|
SUPERLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_in_middle_of_superlist(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([0, 1, 2, 3, 4, 5], [2, 3]),
|
||||||
|
SUPERLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_at_end_of_superlist(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([0, 1, 2, 3, 4, 5], [3, 4, 5]),
|
||||||
|
SUPERLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_first_list_missing_element_from_second_list(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 3], [1, 2, 3]),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_second_list_missing_element_from_first_list(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 2, 3], [1, 3]),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_order_matters_to_a_list(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 2, 3], [3, 2, 1]),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_same_digits_but_different_numbers(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist([1, 0, 1], [10, 1]),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
# additional track specific test
|
||||||
|
def test_unique_return_values(self):
|
||||||
|
self.assertEqual(len(set([SUBLIST, SUPERLIST, EQUAL, UNEQUAL])), 4)
|
||||||
|
|
||||||
|
# additional track specific test
|
||||||
|
def test_inner_spaces(self):
|
||||||
|
self.assertEqual(
|
||||||
|
sublist(['a c'], ['a', 'c']),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
# additional track specific test
|
||||||
|
def test_large_lists(self):
|
||||||
|
list_1 = list(range(1000)) * 1000 + list(range(1000, 1100))
|
||||||
|
list_2 = list(range(900, 1050))
|
||||||
|
self.assertEqual(
|
||||||
|
sublist(list_1, list_2),
|
||||||
|
SUPERLIST
|
||||||
|
)
|
||||||
|
|
||||||
|
# additional track specific test
|
||||||
|
def test_spread_sublist(self):
|
||||||
|
multiples_of_3 = list(range(3, 200, 3))
|
||||||
|
multiples_of_15 = list(range(15, 200, 15))
|
||||||
|
self.assertEqual(
|
||||||
|
sublist(multiples_of_15, multiples_of_3),
|
||||||
|
UNEQUAL
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user