Problem overview
This version of head takes the count as a parameter. The function head(lines: Array[String], n: Int) returns the first n lines of lines joined by newline characters (\n). The lesson guarantees 0 <= n <= lines.length, and for n = 0 the result is the empty string.
For ["alpha", "beta", "gamma", "delta", "epsilon"] and n = 3, the result is "alpha\nbeta\ngamma".
Notice the new parameter type: the lines now arrive as an Array[String] instead of a List[String]. The previous version took a list.
Approach
As before, the job has two parts: pick the lines, then join them with mkString("\n"). What changes is how the lines are picked. Instead of a fixed 10, the caller says how many.
The lesson uses slice(from, until). It returns the items from index from up to, but not including, index until. So lines.slice(0, n) is the items at indices 0, 1, up to n - 1: exactly the first n. For n = 3 that is alpha, beta, gamma. For n = 0 the range is empty, so the slice is empty and mkString gives "".
Because the lesson promises that n never exceeds the length, there is nothing to clamp. The whole function is one expression.
Step by step
- Slice and join in one expression.
lines.slice(0, n)picks indices 0 up to but not includingn, andmkString("\n")puts a newline between each two of them.
The solution in Scala
class Solution {
def head(lines: Array[String], n: Int): String = {
return lines.slice(0, n).mkString("\n")
}
}
Details worth knowing:
- A Scala
Arrayis the JVM's array: fixed in size, with items that can be changed. It has the same collection methods as aList, includingslice,takeandmkString, so the code reads the same for both. - The array's size is
lines.length. AListusessize(andlengthtoo), while an array's usual spelling islength. slicereturns a new array holding copies of the chosen items. Changing the new array does not touch the original.- Printing an array directly does not show its contents.
println(arr)shows a text that starts with[Ljava.lang.String;@followed by a number. To see the items, join them withmkStringor convert withtoList. Here the function returns the joined string, so the result prints properly. - In
slice(0, n)the second argument is an end that is not included. It is not a count of items from the start, although with a start of 0 the two coincide.
Edge cases and common mistakes
n = 0.slice(0, 0)is empty, so the result is"".nequal to the length. With the five lines above andn = 5, every line is kept:"alpha\nbeta\ngamma\ndelta\nepsilon".- An empty array. Only
n = 0is allowed for it, and that gives"". - Off by one.
slice(0, n - 1)drops the last wanted line, andslice(1, n)starts at the second line. - Confusing
slicewithtake. For a prefix the two give the same lines:lines.take(n)is the same aslines.slice(0, n). The lesson usessliceto practise the "from, until" form. - A trailing newline.
mkStringplaces newlines between lines only, so nothing follows the last line.
Testing
With g as the array ["alpha", "beta", "gamma", "delta", "epsilon"]:
| Call | Result |
|---|---|
head(g, 3) | "alpha\nbeta\ngamma" |
head(g, 0) | "" |
head(g, 5) | "alpha\nbeta\ngamma\ndelta\nepsilon" |
The next version builds on this one.
