Problem overview
The head method keeps the signature from version 2: head(self, lines, n). What changes is the promise about n. It may now be larger than the number of lines. In that case the method returns all the lines, without crashing and without padding. For this version n is never negative. When n is 0, or when there are no lines, the result is an empty string.
For ["alpha", "beta", "gamma"] and n = 10, the result is 'alpha\nbeta\ngamma':
alpha
beta
gamma
Approach: let the slice stop at the end
A first instinct is to guard against a too-large n: compare it with len(lines) and use the smaller of the two, or loop and stop at the end of the list. That works, but the code from the previous version needs no change. A slice quietly stops at the end of the list: when the end position lies past the last item, Python simply takes everything up to the last item. lines[:n] with a huge n is a copy of the whole list.
So this lesson is less about writing new code and more about understanding why the same line is already correct. It is worth knowing that slices behave this way, because indexing does not: lines[10] on a three-item list raises IndexError, while lines[:10] never does.
Step by step
The lesson has one step: return the first n lines joined into one string, with a newline between each two. It is the same line as in version 2. Reading it again with the new case in mind: lines[:n] gives at most n items, and fewer when the list is shorter, and "\n".join(...) glues whatever it gets. With 5 lines and n = 8, the slice holds all 5 and the join returns them with four newlines between them.
The solution in Python
class Solution:
def head(self, lines, n):
return "\n".join(lines[:n])
Details behind it:
- Out-of-range slice bounds are clipped. A slice never raises
IndexError. Bounds beyond the list are cut back to its start or end. Only a single index likelines[i]is checked strictly. - "At most" is what a slice means. The result has
min(n, len(lines))items whennis not negative. - No padding. The slice does not add empty lines to reach
n. Five lines withn = 8give five lines, not eight. - The list is not modified. The slice is a new list.
Edge cases and common mistakes
nlarger than the list.head(["a", "b", "c", "d", "e"], 8)returns'a\nb\nc\nd\ne', andhead(["alpha", "beta", "gamma"], 10)returns'alpha\nbeta\ngamma'.- An empty list with any
n.head([], 5)returns'': the slice of an empty list is empty. n = 0. The result is'', as in version 2.- Indexing in place of slicing. Code such as
lines[i]inside a loop up tonwould raiseIndexErrorwhennis too big. The slice avoids the problem without any check. - Adding an unneeded guard. An
if n > len(lines)branch is harmless but redundant here. - Expecting padding or an error. Neither happens: a too-big
nis not an error, and the result is never longer than the input.
Testing
["alpha", "beta", "gamma"]withn = 10gives'alpha\nbeta\ngamma'.["a", "b", "c", "d", "e"]withn = 8gives'a\nb\nc\nd\ne'.[]withn = 5gives''.["a", "b"]withn = 0gives''.
The next version builds on this one.
