Problem overview
This version of head takes a second parameter. Head(List<string> lines, int n) returns the first n lines joined by newline characters. The lesson promises 0 <= n <= lines.Count, and if n is 0 the result is an empty string.
For ["alpha", "beta", "gamma", "delta", "epsilon"] with n = 3 the result is "alpha\nbeta\ngamma".
Approach
The first version kept a fixed number of lines, 10, and had to guard against short lists. Here the caller picks the count, and the promise that n never exceeds the list's size removes the need for a guard. The two steps are still the same: take the first n items, then join them with newlines.
lines.GetRange(0, n) returns a new list of n items starting at index 0. string.Join("\n", ...) glues them with a newline between neighbours and none at the end. Because the arguments of Join can be any expression, both calls fit in one return statement.
Step by step
- Return
string.Join("\n", lines.GetRange(0, n)). The lesson has a single step, because withnguaranteed to be valid nothing needs clamping.GetRange(0, n)is evaluated first and its result is the second argument ofJoin.
The solution in C#
class Solution {
public string Head(List<string> lines, int n) {
return string.Join("\n", lines.GetRange(0, n));
}
}
C# details worth knowing:
GetRange(index, count)takes a start and a count, not a start and an end. With a start of 0 they coincide, which makesGetRange(0, n)read naturally as "the first n".- It copies. The result is a separate
List<string>, so the caller's list is not changed. - It is strict. Unlike slicing in some languages,
GetRangedoes not cut a too-large count down to size. Here, with five lines,n = 6throws anArgumentException, and a negativensuch as-1throws anArgumentOutOfRangeException. The lesson promises neither happens, which is why this version has no guard. - The separator goes first.
string.Join("\n", list)takes the separator, then the items. A list works directly, with no conversion to an array.
Complexity
Time: O(n + length of the kept lines). GetRange copies n references and Join copies the characters of those lines once. The remaining lines of the list are never touched.
Space: O(n + length of the kept lines). The sub-list holds n references and the result is a new string.
Edge cases and common mistakes
n = 0.GetRange(0, 0)is an empty list, and joining it gives"". Forn = 0on the five example lines the result is"".nequal tolines.Count. The whole list comes back:n = 5gives"alpha\nbeta\ngamma\ndelta\nepsilon". The count is allowed to reach the size of the list.n = 1. One line, no newline:"alpha".- An empty list. With
n = 0the result is"". Any largernwould break the lesson's promise, andGetRange(0, 1)on an empty list throws anArgumentException. - A trailing newline. Appending
"\n"after each kept line adds an extra one.string.Joinputs newlines only between lines. - Mixing up count and end index. Because the start is 0 the two agree, but remember that the second argument of
GetRangeis a count.
Testing
- The five example lines with
n = 3give"alpha\nbeta\ngamma". - The same lines with
n = 0give"". - The same lines with
n = 1give"alpha". - The same lines with
n = 5give all five joined:"alpha\nbeta\ngamma\ndelta\nepsilon". - An empty list with
n = 0gives"".
