Start learning free

LESSON 6 OF 69 · PROJECTS: COMMAND-LINE TOOLS

head -n in C#: First n Lines with GetRange

Practice Read the full explanation

Extend your head command to take an integer n and return the first n lines of lines, joined by newline characters (\n). For this version you may assume 0 ≤ n ≤ lines.Count. If n is 0 the result is the empty string.

Function signature:

public string Head(List<string> lines, int n)

Example:

Input: lines = ["alpha", "beta", "gamma", "delta", "epsilon"], n = 3

Output: "alpha\nbeta\ngamma"

When printed:
alpha
beta
gamma

Use lines.GetRange(0, n) for the first n elements, then string.Join("\n", ...). Since the problem guarantees n ≤ lines.Count, no Min needed.

Combine the two calls into one:
return string.Join("\n", lines.GetRange(0, n));


The full solution:
class Solution {
    public string Head(List<string> lines, int n) {
        return string.Join("\n", lines.GetRange(0, n));
    }
}

STEP 1 OF 1
    Ctrl + Enter to check

    C# · PROJECTS: COMMAND-LINE TOOLS

    Head V2 in C#, explained

    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

    1. Return string.Join("\n", lines.GetRange(0, n)). The lesson has a single step, because with n guaranteed to be valid nothing needs clamping. GetRange(0, n) is evaluated first and its result is the second argument of Join.

    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 makes GetRange(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, GetRange does not cut a too-large count down to size. Here, with five lines, n = 6 throws an ArgumentException, and a negative n such as -1 throws an ArgumentOutOfRangeException. 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 "". For n = 0 on the five example lines the result is "".
    • n equal to lines.Count. The whole list comes back: n = 5 gives "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 = 0 the result is "". Any larger n would break the lesson's promise, and GetRange(0, 1) on an empty list throws an ArgumentException.
    • A trailing newline. Appending "\n" after each kept line adds an extra one. string.Join puts 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 GetRange is a count.

    Testing

    • The five example lines with n = 3 give "alpha\nbeta\ngamma".
    • The same lines with n = 0 give "".
    • The same lines with n = 1 give "alpha".
    • The same lines with n = 5 give all five joined: "alpha\nbeta\ngamma\ndelta\nepsilon".
    • An empty list with n = 0 gives "".
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free