Problem overview
The signature is the same as in the previous version, head(_ lines: [String], _ n: Int) -> String, but now n may be larger than the number of lines. In that case the function returns all the lines: it doesn't crash and it doesn't pad. You may assume n >= 0. When n is 0, or there are no lines, the result is "".
For ["alpha", "beta", "gamma"] and n = 10 the result is "alpha\nbeta\ngamma". Asking for ten lines of a three-line input gives the three that exist.
Approach
The previous version used n directly as the end of the slice, which is only valid while n doesn't pass the end of the array. The fix is to clamp: the number of lines to keep is the smaller of n and lines.count. With 5 lines, n = 8 keeps 5 and n = 3 keeps 3. The slice is always inside the array, so the join works for every input.
Step by step
- Clamp the count. Write
let end = min(n, lines.count). This is the only new line compared with the fixed-nversion, and it is what letsnbe as large as the caller likes. - Slice up to
endand join.lines[0..<end]followed byjoined(separator: "\n")works as before, now with a range that can't go past the array.
The solution in Swift
func head(_ lines: [String], _ n: Int) -> String {
let end = min(n, lines.count)
return lines[0..<end].joined(separator: "\n")
}
Swift details worth a closer look:
minworks on any comparable values. Both arguments areInthere:nis one, and so islines.count. The call returns whichever is smaller.- Why not check and branch? You could write an
if n > lines.counttest and handle the two cases separately, butminexpresses the clamp in one line and leaves a single code path. - The empty range again. If
nis 0, or the array is empty,endis 0 and0..<0selects nothing. The result is"", with no special case. - The assumption still matters. The clamp only protects the upper bound. If
nwere negative,minwould return it unchanged, and the range0..<nwould crash at run time, because Swift doesn't allow a range whose upper bound is below its lower bound. The statement rules this input out for this version.
Complexity
The function joins at most lines.count lines, so time is proportional to the total length of the selected lines. The clamp is constant time, and the slice doesn't copy. The extra space is the result string.
Edge cases and common mistakes
nlarger than the count. The result is every line.head(["alpha", "beta", "gamma"], 10)is"alpha\nbeta\ngamma", with no padding for the missing lines.n = 0. The result is"".- Empty array.
head([], 4)is"", sincemin(4, 0)is 0. - Clamping to the wrong value. Using
min(n, lines.count - 1)would drop the last line. The range is half-open, so the count itself is the right upper bound. - Using
maxby mistake.max(n, lines.count)goes past the end of the array whenevernis larger, which is the crash the clamp is meant to prevent.
Testing
| Input | Result (as a Swift literal) |
|---|---|
["alpha", "beta", "gamma"], n = 10 | "alpha\nbeta\ngamma" |
["alpha", "beta", "gamma", "delta", "epsilon"], n = 3 | "alpha\nbeta\ngamma" |
[], n = 4 | "" |
five lines, n = 0 | "" |
