Skip to content
Merged
23 changes: 23 additions & 0 deletions solutions/python/sublist/1/sublist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
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 = None
SUPERLIST = None
EQUAL = None
UNEQUAL = None


def sublist(list_one, list_two):
pass
57 changes: 57 additions & 0 deletions solutions/python/sublist/2/sublist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
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 = "SUBLIST"
SUPERLIST = "SUPERLIST"
EQUAL = "EQUAL"
UNEQUAL = "UNEQUAL"


def sublist(list_one: list, list_two: list) -> str:
"""
Classify the relationship between two lists.

Determines whether ``list_one`` and ``list_two`` are equal, or whether one
is a contiguous sublist of the other, and returns the appropriate constant.

:param list_one: First list to compare.
:type list_one: list
:param list_two: Second list to compare.
:type list_two: list
:returns: One of ``EQUAL``, ``SUBLIST``, ``SUPERLIST``, or ``UNEQUAL``.
:rtype: str
"""
if list_one is list_two or list_one == list_two:
return EQUAL

if len(list_one) == len(list_two) and not (
list_one is list_two or list_one == list_two
):
return UNEQUAL

l1: str = ",".join(str(i) for i in list_one)
l2: str = ",".join(str(i) for i in list_two)

if len(l1) > len(l2):
for i in range(0, len(l1) - len(l2) + 1):
if l1[i:].startswith(l2):
return SUPERLIST

if len(l2) > len(l1):
for i in range(0, len(l2) - len(l1) + 1):
if l2[i:].startswith(l1):
return SUBLIST

return UNEQUAL
45 changes: 39 additions & 6 deletions sublist/sublist.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,44 @@

# Possible sublist categories.
# Change the values as you see fit.
SUBLIST = None
SUPERLIST = None
EQUAL = None
UNEQUAL = None
SUBLIST = "SUBLIST"
SUPERLIST = "SUPERLIST"
EQUAL = "EQUAL"
UNEQUAL = "UNEQUAL"


def sublist(list_one, list_two):
pass
def sublist(list_one: list, list_two: list) -> str:
"""
Classify the relationship between two lists.

Determines whether ``list_one`` and ``list_two`` are equal, or whether one
is a contiguous sublist of the other, and returns the appropriate constant.

:param list_one: First list to compare.
:type list_one: list
:param list_two: Second list to compare.
:type list_two: list
:returns: One of ``EQUAL``, ``SUBLIST``, ``SUPERLIST``, or ``UNEQUAL``.
:rtype: str
"""

len1: int = len(list_one)
len2: int = len(list_two)

if len1 == len2:
if list_one == list_two:
return EQUAL
return UNEQUAL

l1: str = ",".join(str(i) for i in list_one)
l2: str = ",".join(str(i) for i in list_two)

if len1 > len2:
if l2 in l1:
return SUPERLIST

if len2 > len1:
if l1 in l2:
return SUBLIST

return UNEQUAL
2 changes: 1 addition & 1 deletion sublist/sublist_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=C0301
# pylint: disable=C0301, C0114, C0115, C0116, R0904
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/sublist/canonical-data.json
# File last updated on 2023-07-19
Expand Down