From 4b7d79c808767b3cab297df7a59c2d48a471dfe0 Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 24 Jul 2019 00:19:47 -0500 Subject: [PATCH] sublist exercise --- python/sublist/.exercism/metadata.json | 1 + python/sublist/README.md | 63 +++++++++++ python/sublist/sublist.py | 35 ++++++ python/sublist/sublist_test.py | 142 +++++++++++++++++++++++++ 4 files changed, 241 insertions(+) create mode 100644 python/sublist/.exercism/metadata.json create mode 100644 python/sublist/README.md create mode 100644 python/sublist/sublist.py create mode 100644 python/sublist/sublist_test.py diff --git a/python/sublist/.exercism/metadata.json b/python/sublist/.exercism/metadata.json new file mode 100644 index 0000000..e80d5ed --- /dev/null +++ b/python/sublist/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"python","exercise":"sublist","id":"a217429e9ed74c7f8d0e72bb6660d881","url":"https://exercism.io/my/solutions/a217429e9ed74c7f8d0e72bb6660d881","handle":"Xevion","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/python/sublist/README.md b/python/sublist/README.md new file mode 100644 index 0000000..1349a1b --- /dev/null +++ b/python/sublist/README.md @@ -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. diff --git a/python/sublist/sublist.py b/python/sublist/sublist.py new file mode 100644 index 0000000..8d8bfb5 --- /dev/null +++ b/python/sublist/sublist.py @@ -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)) \ No newline at end of file diff --git a/python/sublist/sublist_test.py b/python/sublist/sublist_test.py new file mode 100644 index 0000000..6bc21df --- /dev/null +++ b/python/sublist/sublist_test.py @@ -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()