Problem overview
The Unix head command prints the start of its input. This first version is a function, head, that takes a List[String] and returns the first 10 lines joined by newline characters (\n). If there are fewer than 10 lines, it returns all of them, and for no lines at all it returns the empty string.
For a list of the 13 letters "a" to "m", the result is "a\nb\nc\nd\ne\nf\ng\nh\ni\nj". Lines k, l and m are dropped.
Approach
There are two jobs: choose which lines to keep, then join them. The joining is mkString("\n"), which puts a newline between each pair of neighbours and none after the last. The choosing is the part specific to head.
A list method, take(k), keeps the first k items. The lesson computes how many to ask for before calling it: the smaller of 10 and the number of lines. With 13 lines that number is 10. With 3 lines it is 3, and with 0 lines it is 0. So the call never asks for lines that aren't there, and the empty list needs no special case.
For the three lines "a", "b", "c", take(10) gives List(a, b, c), and take(0) gives List(). In Scala take never fails when it is asked for more than the list holds, so the math.min is a clear statement of intent more than a safety net.
Step by step
- Compute the count.
val end = math.min(10, lines.size)is the smaller of 10 and the number of lines: 10 for 13 lines, 3 for 3 lines, 0 for none. - Take and join.
lines.take(end)keeps the firstendlines, andmkString("\n")glues them. This is the last expression of the method, so it is the value the method returns and noreturnis needed.
The solution in Scala
class Solution {
def head(lines: List[String]): String = {
val end = math.min(10, lines.size)
lines.take(end).mkString("\n")
}
}
Details worth knowing:
math.minis a function in thescala.mathpackage, which Scala makes available asmathwithout an import. It works onInt,Long,Doubleand the other number types.lines.sizecounts the items. On aListit walks the whole list, which is fine for a few lines.takereturns a new list and leaveslinesunchanged. AListis immutable, so every such method gives a new value.- The method body is a block, and a block's value is its last expression. That is why
lines.take(end).mkString("\n")sits on its own line withoutreturn. Writingreturnin front would behave the same.
Edge cases and common mistakes
- Fewer than 10 lines.
List("a", "b", "c")gives"a\nb\nc": all three lines, with no padding to reach 10. - Exactly 10 and 11 lines. Both give the same ten lines,
"a\nb\nc\nd\ne\nf\ng\nh\ni\nj", because for 11 lines the eleventh is cut and for 10 nothing is. - No lines.
List()gives"", sincetake(0)of an empty list is empty andmkStringon it returns an empty string. - Counting from the wrong place.
take(10)keeps 10 lines, indices 0 to 9. Usingtake(9)ortake(11)is the usual off-by-one. - A trailing newline. Joining with
mkString("\n")leaves nothing after the last kept line. Appending a newline per line in a loop would leave one.
Testing
| Input | Result |
|---|---|
the 13 letters "a" to "m" | "a\nb\nc\nd\ne\nf\ng\nh\ni\nj" |
| the first 11 of those letters | "a\nb\nc\nd\ne\nf\ng\nh\ni\nj" |
List("a", "b", "c") | "a\nb\nc" |
List() | "" |
The next version builds on this one.
