Problem overview
This version adds the -v flag of grep. The method grepInvert(self, lines, pattern) returns the lines that do not contain pattern as a substring, in their original order, joined with newline characters. The match is case-sensitive.
With pattern = "err" and lines = ["error", "ok", "another error", "fine"] the result is "ok\nfine". If every line contains the pattern, nothing is kept and the result is the empty string.
Approach: flip the condition
The previous version kept a line when pattern in line was true. This version keeps it when that test is false. Python has an operator for exactly that: pattern not in line. It is the same as not (pattern in line), written the way you would say it.
Everything else stays the same: an empty result list, one loop over the lines, an append for each line that passes, and a join at the end.
Step by step
- Create the empty list
result. It will hold the lines that survive. - Loop over
linesso every line gets the same check. - Test
pattern not in line. It is true when the pattern appears nowhere in the line. Lines that contain the pattern fail the test and are skipped. - Append
linetoresultinside theif. Only lines without the pattern get there. - Return
"\n".join(result). When every line contained the pattern, the list is empty and the join gives"", which is the answer the lesson wants.
The solution in Python
class Solution:
def grepInvert(self, lines, pattern):
result = []
for line in lines:
if pattern not in line:
result.append(line)
return "\n".join(result)
not in is a single operator, written with two words. Writing if not pattern in line also works, because in binds tighter than not, but not in is the form to prefer because it reads like the sentence it expresses.
One special case follows from the definition: every string contains the empty string, so with pattern = "" every line fails the not in test and the result is "".
Tracing the example, line by line, shows how the filter works:
| line | "err" not in line | kept |
|---|---|---|
"error" | False | no |
"ok" | True | yes |
"another error" | False | no |
"fine" | True | yes |
The list holds ["ok", "fine"] at the end, and joining it gives "ok\nfine". The relative order of the kept lines never changes, because the loop visits the lines in order and appends as it goes.
Don't confuse this test with line != pattern. That compares the whole line with the pattern, so it would keep "error": the line isn't equal to "err", although it contains it. The lesson asks about containment, so the operator is not in.
Complexity
Time is proportional to the total length of the lines, since each line is searched once. Extra space is the list of kept lines and the final string.
Edge cases and common mistakes
- Every line matches. The result is
"". For["error", "an error"]and"err"nothing is kept. - An empty list of lines. The loop doesn't run and the result is
"". - Using
inby habit. Copying the previous version unchanged returns the opposite set of lines. - Case. The test is case-sensitive, so
"ERROR"doesn't contain"err"and is kept. - Returning the list. The function returns one string joined with
"\n".
Testing
| pattern | lines | result |
|---|---|---|
"err" | ["error", "ok", "another error", "fine"] | "ok\nfine" |
"err" | ["error", "an error"] | "" |
"err" | [] | "" |
"" | ["a", "b"] | "" |
