Files
exercism_puzzles/python/sublist/sublist.py
T

41 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
This exercise stub and the test suite contain several enumerated constants.
Enumerated constants can be done with 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 = 1
SUPERLIST = 2
EQUAL = 3
UNEQUAL = 4
def sublist(list_one: list, list_two: list):
# test for equality of lists
if list_one == list_two:
return EQUAL
# test for list_one is a sublist of list_two
if len(list_one) < len(list_two):
if any(
list_two[i : i + len(list_one)] == list_one
for i in range(len(list_two) - len(list_one) + 1)
):
return SUBLIST
# test for list_one is a superlist of list_two
if len(list_one) > len(list_two):
if any(
list_one[i : i + len(list_two)] == list_two
for i in range(len(list_one) - len(list_two) + 1)
):
return SUPERLIST
# previous tests failed, so lists are UNEQUAL
return UNEQUAL