Skip to content

Commit 33bca44

Browse files
authored
Merge pull request #138 from ikostan/main
Merge from master
2 parents c24325a + a2aeb77 commit 33bca44

File tree

4 files changed

+120
-7
lines changed

4 files changed

+120
-7
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 = None
17+
SUPERLIST = None
18+
EQUAL = None
19+
UNEQUAL = None
20+
21+
22+
def sublist(list_one, list_two):
23+
pass
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 = "SUBLIST"
17+
SUPERLIST = "SUPERLIST"
18+
EQUAL = "EQUAL"
19+
UNEQUAL = "UNEQUAL"
20+
21+
22+
def sublist(list_one: list, list_two: list) -> str:
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: str
35+
"""
36+
if list_one is list_two or list_one == list_two:
37+
return EQUAL
38+
39+
if len(list_one) == len(list_two) and not (
40+
list_one is list_two or list_one == list_two
41+
):
42+
return UNEQUAL
43+
44+
l1: str = ",".join(str(i) for i in list_one)
45+
l2: str = ",".join(str(i) for i in list_two)
46+
47+
if len(l1) > len(l2):
48+
for i in range(0, len(l1) - len(l2) + 1):
49+
if l1[i:].startswith(l2):
50+
return SUPERLIST
51+
52+
if len(l2) > len(l1):
53+
for i in range(0, len(l2) - len(l1) + 1):
54+
if l2[i:].startswith(l1):
55+
return SUBLIST
56+
57+
return UNEQUAL

sublist/sublist.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,44 @@
1313

1414
# Possible sublist categories.
1515
# Change the values as you see fit.
16-
SUBLIST = None
17-
SUPERLIST = None
18-
EQUAL = None
19-
UNEQUAL = None
16+
SUBLIST = "SUBLIST"
17+
SUPERLIST = "SUPERLIST"
18+
EQUAL = "EQUAL"
19+
UNEQUAL = "UNEQUAL"
2020

2121

22-
def sublist(list_one, list_two):
23-
pass
22+
def sublist(list_one: list, list_two: list) -> str:
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: str
35+
"""
36+
37+
len1: int = len(list_one)
38+
len2: int = len(list_two)
39+
40+
if len1 == len2:
41+
if list_one == list_two:
42+
return EQUAL
43+
return UNEQUAL
44+
45+
l1: str = ",".join(str(i) for i in list_one)
46+
l2: str = ",".join(str(i) for i in list_two)
47+
48+
if len1 > len2:
49+
if l2 in l1:
50+
return SUPERLIST
51+
52+
if len2 > len1:
53+
if l1 in l2:
54+
return SUBLIST
55+
56+
return UNEQUAL

sublist/sublist_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pylint: disable=C0301
1+
# pylint: disable=C0301, C0114, C0115, C0116, R0904
22
# These tests are auto-generated with test data from:
33
# https://github.com/exercism/problem-specifications/tree/main/exercises/sublist/canonical-data.json
44
# File last updated on 2023-07-19

0 commit comments

Comments
 (0)