Problem overview
The Unix cat command prints text, one line after another. In this first version you build its core as a function: given a list of lines, return one string in which the lines are separated by newline characters (\n). Printing that string shows one line per row.
The signature is fun cat(lines: List<String>): String. For ["hello", "world", "foo"] the result is "hello\nworld\nfoo". Blank lines count as lines, and an empty list gives an empty string.
Approach
You could loop over the list, append each line and a newline, and then worry about the extra newline at the end. Kotlin already has the operation you want. joinToString glues the elements of a collection into one string and puts a separator between each two of them, never before the first or after the last. The separator here is "\n".
Step by step
- Call
joinToString("\n")onlinesand return the result. That is the whole lesson. The argument is the separator. Because it goes only between two lines, the last line gets no newline after it.
The solution in Kotlin
class Solution {
fun cat(lines: List<String>): String {
return lines.joinToString("\n")
}
}
Calling it on a few inputs (shown with \n written out) gives:
| lines | result |
|---|---|
["hello", "world", "foo"] | "hello\nworld\nfoo" |
["a", "", "b"] | "a\n\nb" |
["only"] | "only" |
[] | "" |
Printing the result
The function returns a string; it doesn't print anything itself. To see rows, pass the result to println:
val text = Solution().cat(listOf("hello", "world", "foo"))
println(text)
println(text.length)
println(Solution().cat(listOf("x", "y")) + "|")
This prints:
hello
world
foo
15
x
y|
The length is 15 because the string holds 5 + 5 + 3 letters plus two newline characters. The | after y shows that nothing follows the last line: no trailing newline was added, so the bar sits right after it.
Kotlin details worth knowing
- The separator is the first parameter.
joinToStringhas other optional parameters (a prefix, a postfix and more), so passing one string means the separator. Without any argument the separator would be", ", which would givehello, world, foo. - It works on any collection.
linesis a read-onlyList<String>, and you don't need to copy it into anything mutable first. - The empty list is handled for free. Joining nothing gives
"", with no special case in your code. "\n"is one character. In a Kotlin string literal the backslash and thentogether stand for a single newline character.
Edge cases and common mistakes
- A trailing newline. A loop that appends
line + "\n"for each line leaves a newline after the last one, and the result no longer matches"hello\nworld\nfoo". Separator-based joining avoids this. - Blank lines. An empty string is still an element, so
["", ""]gives"\n": two empty lines with one separator between them. Don't filter empty strings out. - A single line. There is nothing to separate, so the line comes back unchanged and without a newline.
- Forgetting the argument.
lines.joinToString()compiles, but it joins with commas and spaces.
Try these inputs in the editor above. The next version builds on this function.
