Skip to content

Commit 2dd97d8

Browse files
authored
Merge pull request #147 from ikostan/exercism-sync/48dd8c866dd41a24
[Sync Iteration] python/sublist/7
2 parents d78cd98 + 9e63e81 commit 2dd97d8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
This exercise stub and the test suite contain several enumerated constants.
3+
4+
Enumerated constants can be done with a NAME assigned to an arbitrary,
5+
but unique value. An integer is traditionally used because it’s memory
6+
efficient.
7+
8+
It is a common practice to export both constants and functions that work with
9+
those constants (ex. the constants in the os, subprocess and re modules).
10+
11+
You can learn more here: https://en.wikipedia.org/wiki/Enumerated_type
12+
"""
13+
14+
# Possible sublist categories.
15+
# Change the values as you see fit.
16+
SUBLIST = 0
17+
SUPERLIST = 1
18+
EQUAL = 2
19+
UNEQUAL = 3
20+
21+
22+
def sublist(list_one: list, list_two: list) -> int:
23+
"""
24+
Classify the relationship between two lists.
25+
26+
Determines whether ``list_one`` and ``list_two`` are equal, or whether one
27+
is a contiguous sublist of the other, and returns the appropriate constant.
28+
29+
:param list_one: First list to compare.
30+
:type list_one: list
31+
:param list_two: Second list to compare.
32+
:type list_two: list
33+
:returns: One of ``EQUAL``, ``SUBLIST``, ``SUPERLIST``, or ``UNEQUAL``.
34+
:rtype: int
35+
"""
36+
37+
if len(list_one) == len(list_two):
38+
if list_one == list_two:
39+
return EQUAL
40+
return UNEQUAL
41+
42+
l1: str = ",".join(str(i) for i in list_one)
43+
l2: str = ",".join(str(i) for i in list_two)
44+
45+
if l2 in l1:
46+
return SUPERLIST
47+
48+
if l1 in l2:
49+
return SUBLIST
50+
51+
return UNEQUAL

0 commit comments

Comments
 (0)