Problem overview
The Unix head command prints the beginning of a text. By default it shows the first 10 lines. In this first version, Head receives a List<string> of lines and returns the first 10 of them joined by newline characters. If the list has fewer than 10 lines, all of them are returned. If there are no lines, the result is an empty string.
For the 13 lines "a" to "m", the result is "a\nb\nc\nd\ne\nf\ng\nh\ni\nj": the letters a through j, one per row when printed. For a list of 3 lines, all 3 come back.
Approach
There are two parts: cut the list down to at most 10 items, then join them with newlines.
For the cut, List<T>.GetRange(index, count) returns a new list with count items starting at index. Starting at 0 with a count of 10 gives the first 10 lines. The catch is the count: GetRange fails when it asks for more items than the list has. So 10 is only safe when the list has at least 10 items.
The safe count is the smaller of 10 and the number of lines. Math.Min(10, lines.Count) gives that. With 13 lines it is 10. With 3 lines it is 3, so the method never asks for lines that aren't there. With an empty list it is 0, and a range of zero items is valid and empty.
For the join, string.Join("\n", ...) puts a newline between neighbouring lines and none after the last. An empty range joins to "", so the empty case needs no code of its own.
Step by step
- Compute
endasMath.Min(10, lines.Count). It is the number of lines to keep.Math.Minreturns the smaller of its two arguments. - Return the joined range:
string.Join("\n", lines.GetRange(0, end)).GetRange(0, end)is the firstenditems, andJoininserts the newline between each two of them. Sinceendnever exceedslines.Count, the range is always valid.
The solution in C#
class Solution {
public string Head(List<string> lines) {
int end = Math.Min(10, lines.Count);
return string.Join("\n", lines.GetRange(0, end));
}
}
C# details worth knowing:
- The second argument of
GetRangeis a count, not an end index. The variable is calledendhere because, starting at index 0, the count and the end position are the same number. With a start other than 0, they would differ. GetRangereturns a new list. It copies the references of the selected items into a separateList<string>. Changing the new list does not changelines.- Out-of-range ranges throw. Asking for more items than exist throws an
ArgumentException, and a negative count throwsArgumentOutOfRangeException. C# doesn't clamp for you the way slicing does in some other languages, which is whyMath.Minis needed. Mathneedsusing System;in a file of your own, andList<string>needsusing System.Collections.Generic;.Count, notLength. AList<T>reports its size with theCountproperty.
Complexity
Time: O(1) for the cut, because at most 10 items are copied, and O(total length of those lines) for the join, which copies their characters once. The size of the original list doesn't matter.
Space: O(total length of the kept lines). The sub-list holds at most 10 references and the result is a new string with the kept text.
Edge cases and common mistakes
- Fewer than 10 lines. Calling
GetRange(0, 10)directly on a 3-line list throws. TheMath.Minavoids it. - Exactly 10 lines. All 10 come back, and with 11 lines the 11th is dropped: a list
"L1"to"L11"gives"L1"to"L10". - An empty list.
endis 0,GetRange(0, 0)is an empty list and the result is"". - A single line.
["L1"]gives"L1", with no newline. - Writing 9 instead of 10. The bound is a count of 10 lines, indexes 0 to 9. Keep
GetRange(0, end)and don't subtract one. - A trailing newline. Appending
"\n"after every line would add one at the end.string.Joindoesn't.
Testing
- The 13 lines
"a"to"m"give"a\nb\nc\nd\ne\nf\ng\nh\ni\nj". - Three lines
"L1","L2","L3"give"L1\nL2\nL3". - Ten lines
"L1"to"L10"give all ten:"L1\nL2\nL3\nL4\nL5\nL6\nL7\nL8\nL9\nL10". - Eleven lines
"L1"to"L11"give the same ten. []gives"".
