Problem overview
The Unix head command shows the beginning of a file, ten lines by default. The function head(_ lines: [String]) -> String takes an array of lines and returns the first 10, joined by newline characters. With fewer than 10 lines it returns all of them, and with no lines it returns the empty string.
With the thirteen lines "a" to "m" the result is "a\nb\nc\nd\ne\nf\ng\nh\ni\nj", which prints as the letters a to j, one per line. With only ["x", "y", "z"] the result is "x\ny\nz", and with [] it is "".
Approach
The task has two parts: choose how many lines to keep, then join them. The number to keep is 10 unless the array is shorter, which is exactly min(10, lines.count).
That count is what makes the slice safe. Asking for lines[0..<10] on an array of three elements crashes, because the range reaches past the end. Capping the upper bound with min means the range never asks for lines that aren't there, and the same code handles 13 lines, 3 lines and none.
Step by step
- Compute the end of the range as
let end = min(10, lines.count). With 13 lines it is 10; with 3 lines it is 3; with none it is 0.minis a global function that returns the smaller of two values. - Slice and join.
lines[0..<end]selects the items from index 0 up to but not includingend, andjoined(separator: "\n")glues them with a newline between each two. The result has no newline after the last line.
The solution in Swift
func head(_ lines: [String]) -> String {
let end = min(10, lines.count)
return lines[0..<end].joined(separator: "\n")
}
Swift details worth a closer look:
- A range subscript gives an
ArraySlice, not an array.lines[0..<end]has typeArraySlice<String>: a view onto part of the original array.joined(separator:)works on a slice as it does on an array, so no conversion is needed here. - The half-open range.
0..<endexcludesend, so it holds exactlyendpositions. The closed range0...endwould include one too many, and would crash whenendequals the count. - An empty range is fine.
0..<0is valid and selects nothing, so an empty array needs no special case. Joining no strings gives"". endis just a name. It isn't reserved in Swift. The constant is declared withletbecause it is never changed after being set.
Complexity
At most 10 lines are joined, so the join takes time proportional to the total length of those lines and doesn't depend on how many lines the input holds. The slice shares the original array's storage rather than copying it. Extra space is the output string.
Edge cases and common mistakes
- Fewer than 10 lines. Without the
min, the slicelines[0..<10]on a short array crashes the program with an out-of-range error. - Exactly 10 lines.
min(10, 10)is 10 and all ten lines come back. Ten lines give the same result as the first ten of a longer array. - Empty input. The result is
"". - Off by one. Writing
0...endormin(9, lines.count)returns 11 or 9 lines instead of 10. - A trailing newline. The newline belongs between lines. Adding one after the last line gives output that doesn't match the lesson's expected strings.
Testing
| Input | Result (as a Swift literal) |
|---|---|
thirteen lines, "a" to "m" | "a\nb\nc\nd\ne\nf\ng\nh\ni\nj" |
["x", "y", "z"] | "x\ny\nz" |
[] | "" |
