You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/practice/sublist/.approaches/introduction.md
+15-10Lines changed: 15 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,13 @@
1
1
# Introduction
2
-
There are two broad ways to solve Sublist.
2
+
3
+
There are two broad ways to solve Sublist.
3
4
4
5
## General guidance
5
-
To write the code, you need to branch out (probably with `if`) into the four different possible conditions, and return the appropriate name of the category.
6
+
7
+
To write the code, you need to branch out (probably with `if`) into the four different possible conditions, and return the appropriate name of the category.
6
8
7
9
## Approach: list manipulation
10
+
8
11
The direct approach would be to manipulate and check the given lists to solve this.
9
12
This solution uses a helper function, which simplifies things, but the approach can be implemented without it.
We first check for equality using the `==` operator, if so, then we return `EQUAL`.
27
-
A common way to do this differently would be to return `1` directly, but this is better practice as we [remove magic values][magic values].
27
+
We first check for equality using the `==` operator, if so, then we return `EQUAL`.
28
+
A common way to do this differently would be to return `1` directly, but this is better practice as we [remove magic values][magic values].
28
29
29
30
After that we call `check_sub_sequences` passing in `list_one` and `list_two`.
30
31
In the helper function, we check if `any` of the possible sub-sequences in `list_two` of length `n1` (the length of the first list) are equal to the first list.
31
-
If so, then we conclude that `list_one` is a `SUBLIST` of `list_two`.
32
+
If so, then we conclude that `list_one` is a `SUBLIST` of `list_two`.
32
33
33
34
To find whether `list_one` is a `SUPERLIST` of `list_two`, we just reverse this process - pass in the lists in the opposite order.
34
35
Thus, we check if `any` of the possible sub-sequences in `list_one` of length `n2` (the length of the second list) are equal to the second list.
35
36
36
37
If none of the above conditions are true, we conclude that the two lists are unequal.
In this approach, we convert the lists to strings, so `[1, 2, 3]` becomes `"[1, 2, 3]"`, remove the brackets `"1, 2, 3"`, and add a comma `"1, 2, 3,"`.
31
+
In this approach, we convert the lists to strings, so `[1, 2, 3]` becomes `"[1, 2, 3]"`, remove the brackets `"1, 2, 3"`, and add a comma `"1, 2, 3,"`.
31
32
We check equality and then use the `in` operator to check for `SUBLIST` or `SUPERLIST`, and finally return `UNEQUAL`.
32
33
33
-
We add a comma because, say, we call `sublist` with `[1, 2]` and `[1, 22]`. `"1, 2" in "1, 22"` evaluates to `True`, so
34
-
the **function would wrongly mark it as `SUBLIST`**.
34
+
We add a comma because, say, we call `sublist` with `[1, 2]` and `[1, 22]`. `"1, 2" in "1, 22"` evaluates to `True`, so the **function would wrongly mark it as `SUBLIST`**.
35
+
36
+
This case can be handled by changing the code like this:
37
+
38
+
```python
39
+
list_one_check =str(list_one).strip("[]") +","
40
+
list_two_check =str(list_two).strip("[]") +","
41
+
```
42
+
43
+
Yet, even though the code would pass all of the tests in the Exercism test suite, it would still fail in some cases.
44
+
For example, if we call `sublist` with `[1, 2]` and `[5, "1, 2,", 7]`, the function would return `SUBLIST` when it should actually return `UNEQUAL`.
45
+
46
+
This could be avoided by changing the code to use a separator that isn't the default one:
35
47
36
-
This test can be overridden by changing the code like this:
37
48
```python
38
-
list_one_check =str(list_one).strip("[]")+','
39
-
list_two_check =str(list_two).strip("[]")+','
49
+
list_one_check ="|".join(str(item) for item inlist_one) +"|"
50
+
list_two_check ="|".join(str(item) for item inlist_two) +"|"
40
51
```
41
-
Yet, the test case (which doesn't exist in the Exercism test suite) `["1", "2"]` and `["5", "'1', '2',", "7"]` would
42
-
fail.
43
52
44
-
Students can add any arbitrary string into the representation to try to "defeat" this test - `list_one_check = str
45
-
(list_one) + TOKEN`. The test suite currently test `TOKEN = ''`, but not others.
53
+
However, this only avoids the (theoretical) test and does not fix the solution. For example, a test with the inputs `[1, 2]` and `[5, "1|2|", 7]` would now fail.
No matter what separator is chosen, there will always be at least one input for which the function will return the wrong result. **This is why no approach that converts the lists to strings can ever be correct for all possible inputs.**
0 commit comments