sublist exercise

This commit is contained in:
Xevion
2019-07-24 00:19:47 -05:00
parent b2caf9832e
commit 4b7d79c808
4 changed files with 241 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
"""
This exercise stub and the test suite contain several enumerated constants.
Since Python 2 does not have the enum module, the idiomatic way to write
enumerated constants has traditionally been a NAME assigned to an arbitrary,
but unique value. An integer is traditionally used because its 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 = 0
SUPERLIST = 1
EQUAL = 2
UNEQUAL = 3
def subfinder(mylist, pattern):
matches = []
for i, val in enumerate(mylist):
if val == pattern[0] and mylist[i:i+len(pattern)] == pattern:
matches.append(pattern)
return matches
def sublist(one, two):
if one == two: return EQUAL
if not one or not two: return SUBLIST if not one else SUPERLIST if not two else UNEQUAL
return SUPERLIST if subfinder(one, two) else SUBLIST if subfinder(two, one) else UNEQUAL
x, y = [1, 2, 4], [5, 6, 1, 1, 2, 4, 7, 8]
print(sublist(x, y))
print(sublist(y, x))