Sublist - What to do?

I am genuinely confused as to what is required in this exercise. I unlocked it after completing “All your base” but I have no idea what to use the constants for or what I’m even supposed to return.
I think this is me just being a dolt but is it possible for anyone here to explain what the exercise is about?

The test has

from sublist import (
    sublist,
    SUBLIST,
    SUPERLIST,
    EQUAL,
    UNEQUAL,
)

We need to define a function and four constants. It is up to us how we define the constants, for example they could be four strings "SUBLIST", "SUPERLIST", ... or four numbers like 1, 2, ...

The function sublist() is passed in two lists, and should return one of the constants.

If A = [3, 4] and B = [1, 2, 3, 4, 5], then A is a sublist of B

If A = [1, 2, 3, 4, 5] and B = [2, 3, 4], then A is a superlist of B

We are checking if one of the lists appears within the other list.

1 Like

Hello @keegopeo

The exercise asks to compare two lists.

First, let’s just look at the expected result. We don’t even need to thing that the inputs are lists.

What if we needed to say if some two objects are equal?

def are_equal(a, b):
    return ...

What type would it make sense to return? Obviously, this would be a bool: the lists are either equal or not, we have two possible values.

What if we had to say if a is less then b? Or a is included in b (whatever that means for some objects)? Still, we would have two possible answers, so a bool would still make sense.

But in this exercise, we have to determine multiple possible cases. Back to lists, we’d have to be able to say that list1 is equal to list2, but if not, we need to be able to say that list1 is included in list2, or vice-versa, or, if neither is true, we have to be able to say that the two lists not related / not equal.

So we have 4 possible outcomes (values) that we need to be able to return.

One possibility would be to return some strings: "equal", "sublist", "superlist", "unequal". Yet, we could equally return some int: 0 meaning equal, 1 meaning sublist, and so on. Since there is no obvious choice for a type with unambiguous four values, any four distinct objects (they could even be of different types, at a stretch), that do not compare equal between themselves, could be just as good – as long as the caller of our function knows what those values mean.

The exercise chose to let us determine what the values are. Thus, our module includes four “global” variables (defined at the top level of our module) aptly named EQUAL, SUBLIST, etc. Their actual value does not matter, because the caller (in this case, the tests), will import the values from our module:

from sublist import (
    sublist,
    SUBLIST,
    SUPERLIST,
    EQUAL,
    UNEQUAL,
)

if sublist([1, 2], [1, 2, 3]) == SUBLIST):
    print("yey, [1,2] it's indeed a sublist of [1,2,3]")

To this snippet of code, the four concrete values EQUAL, SUBLIST, etc, are not important (as long as they are distinct). Sometimes people say they are “opaque”, as we should not care what the actual values are, and we should not depend in any way on their concrete values. The sublist module should be able to change those values any way it likes, because me, as a user, should always import them and use them as they are.

Hopefully the rest is more clear:

  • two lists are equal if they contain the same number of elements, and each element from the first list is equal to its corresponding element in the second – we’d need to return EQUAL in this case
  • the first list is a sublist of the second if all its elements are contained, in the same order, in the second: [1,2] is a sublist of: [1,2,3], [0,1,23] but not of [1,0,2]. If it is a sublist, we’d return SUBLIST
  • superlist is just the reverse (so if list1 is a sublist of list2, list2 is a superlist of list1)
  • if the two lists are not equal and neigher is a sublist/superlist of the other, we’d need to return UNEQUAL.

Hope this clarifies it a bit.

Alright. The only thing that was bugging me was what to return. I think I can try the exercise now, thanks.
Edit: Managed to submit and mark as complete. Thanks!

"""
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_two):
    if list_one == list_two:
        return "equal"
    if list_one == []:
        return "sublist"
    if list_two == []:
        return "superlist"
    counter = 0
    if len(list_one) > len(list_two):
        for item in list_one:
            if item == list_two[0] and len(list_one[counter:]) >= len(list_two):
                if list_one[counter: counter + len(list_two)] == list_two:
                    return "superlist"
            counter += 1
    if len(list_two) > len(list_one):
        for item in list_two:
            if item == list_one[0] and len(list_two[counter:]) >= len(list_one):
                if list_two[counter: counter + len(list_one)] == list_one:
                    return "sublist"
            counter += 1 
    return "unequal"
1 Like