 # rootulp's solution

## to Sublist in the Python Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

``````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):

• 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 the help page.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### sublist_test.py

``````import unittest

from sublist import check_lists, SUBLIST, SUPERLIST, EQUAL, UNEQUAL

# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class SublistTest(unittest.TestCase):
def test_unique_return_values(self):
self.assertEqual(len(set([SUBLIST, SUPERLIST, EQUAL, UNEQUAL])), 4)

def test_empty_lists(self):
self.assertEqual(
check_lists([], []),
EQUAL
)

def test_empty_list_within_non_empty_list(self):
self.assertEqual(
check_lists([], [1, 2, 3]),
SUBLIST
)

def test_non_empty_list_contains_empty_list(self):
self.assertEqual(
check_lists([1, 2, 3], []),
SUPERLIST
)

def test_list_equals_itself(self):
self.assertEqual(
check_lists([1, 2, 3], [1, 2, 3]),
EQUAL
)

def test_different_lists(self):
self.assertEqual(
check_lists([1, 2, 3], [2, 3, 4]),
UNEQUAL
)

def test_false_start(self):
self.assertEqual(
check_lists([1, 2, 5], [0, 1, 2, 3, 1, 2, 5, 6]),
SUBLIST
)

def test_consecutive(self):
self.assertEqual(
check_lists([1, 1, 2], [0, 1, 1, 1, 2, 1, 2]),
SUBLIST
)

def test_sublist_at_start(self):
self.assertEqual(
check_lists([0, 1, 2], [0, 1, 2, 3, 4, 5]),
SUBLIST
)

def test_sublist_in_middle(self):
self.assertEqual(
check_lists([2, 3, 4], [0, 1, 2, 3, 4, 5]),
SUBLIST
)

def test_sublist_at_end(self):
self.assertEqual(
check_lists([3, 4, 5], [0, 1, 2, 3, 4, 5]),
SUBLIST
)

def test_at_start_of_superlist(self):
self.assertEqual(
check_lists([0, 1, 2, 3, 4, 5], [0, 1, 2]),
SUPERLIST
)

def test_in_middle_of_superlist(self):
self.assertEqual(
check_lists([0, 1, 2, 3, 4, 5], [2, 3]),
SUPERLIST
)

def test_at_end_of_superlist(self):
self.assertEqual(
check_lists([0, 1, 2, 3, 4, 5], [3, 4, 5]),
SUPERLIST
)

def test_first_list_missing_element_from_second_list(self):
self.assertEqual(
check_lists([1, 3], [1, 2, 3]),
UNEQUAL
)

def test_second_list_missing_element_from_first_list(self):
self.assertEqual(
check_lists([1, 2, 3], [1, 3]),
UNEQUAL
)

def test_order_matters_to_a_list(self):
self.assertEqual(
check_lists([1, 2, 3], [3, 2, 1]),
UNEQUAL
)

def test_same_digits_but_different_numbers(self):
self.assertEqual(
check_lists([1, 0, 1], [10, 1]),
UNEQUAL
)

def test_inner_spaces(self):
self.assertEqual(
check_lists(['a c'], ['a', 'c']),
UNEQUAL
)

def test_large_lists(self):
l1 = list(range(1000)) * 1000 + list(range(1000, 1100))
l2 = list(range(900, 1050))
self.assertEqual(check_lists(l1, l2), SUPERLIST)

multiples_of_3 = list(range(3, 200, 3))
multiples_of_15 = list(range(15, 200, 15))
self.assertEqual(check_lists(multiples_of_15, multiples_of_3), UNEQUAL)

if __name__ == '__main__':
unittest.main()``````
``````SUBLIST = 0
SUPERLIST = 1
EQUAL = 2
UNEQUAL = 3

def check_lists(l1, l2):
if l1 == l2:
return EQUAL
elif l1 in each_cons(l2, len(l1)):
return SUBLIST
elif l2 in each_cons(l1, len(l2)):
return SUPERLIST
else:
return UNEQUAL

# Not the most efficent
def each_cons(lst, size):
return [lst[i: i + size] for i in range(len(lst) - size + 1)]``````