Problem overview
This version adds the -v flag, which inverts the match. The function grepInvert(lines: Array<String>, pattern: String): String returns every line that does not contain pattern as a substring. The test is case-sensitive, the surviving lines keep their original order, and they are joined by newline characters. If every line contains the pattern, the result is the empty string.
With pattern = "err" and lines = ["error", "ok", "another error", "fine"] the result is "ok\nfine". Printed:
ok
fine
Approach: flip the test
Version 1 of grep kept the lines where contains was true. This version keeps the lines where it is false. The loop, the result list and the join stay the same, and the only change is a ! in front of the condition. That is the whole idea of the lesson.
One thing is new on the surface: the parameter is an Array<String> instead of a List<String>. For this function it makes no difference to the body, because a for loop works over both. It only matters to whoever calls the function, which is why the examples below use arrayOf.
Step by step
- Create the result list.
mutableListOf<String>()in avalnamedresultcollects the lines to keep. - Loop over the lines with
for (line in lines)and close the brace right away. - Negate the check. Inside the loop, the
ifuses!in front ofline.contains(pattern). The condition is now true exactly when the pattern is absent. - Keep the line. Inside the
if,result.add(line)stores it. Only a line without the pattern gets here. - Join and return.
result.joinToString("\n")builds the answer. If every line contained the pattern, nothing was kept, and joining an empty list gives the empty string the task asks for.
The solution in Kotlin
class Solution {
fun grepInvert(lines: Array<String>, pattern: String): String {
val result = mutableListOf<String>()
for (line in lines) {
if (!line.contains(pattern)) {
result.add(line)
}
}
return result.joinToString("\n")
}
}
Kotlin details to know:
!is the logical NOT operator. It turns aBooleaninto its opposite, so!line.contains(pattern)reads as "the line doesn't contain the pattern". The!applies to the whole call, because the call binds tighter than the operator.Array<String>andList<String>are different types. A function that takes anArray<String>can't be called withlistOf("a"): the compiler reports that the actual type isList<String>butArray<String>was expected. Build the argument witharrayOf("a", "b"), or turn a list into an array withtoTypedArray().- The result is still a
MutableList, even though the input is an array, since the array's size can't change but the list needs to grow. joinToString("\n")puts the separator between items only, so there is no newline after the last line.
Edge cases and common mistakes
- Case still matters. With pattern
"err", the line"Err"doesn't contain it, so it is kept:["Err", "ok"]returns both lines. - Substrings anywhere.
"xerr"contains"err"in the middle, so it is dropped. - Nothing to remove. If no line contains the pattern, every line comes back, in the original order.
- Everything removed. The result is
"", with no stray newline. - Forgetting the
!. Without it the function is the first version ofgrepand returns the opposite set of lines.
Testing
These results come from running the reference solution on arrays:
| lines | pattern | result |
|---|---|---|
["error", "ok", "another error", "fine"] | "err" | ok and fine, on two lines |
["error", "xerr"] | "err" | the empty string |
["a", "b"] | "z" | a and b, on two lines |
["Err", "ok"] | "err" | Err and ok, on two lines |
