Start learning free

LESSON 5 OF 69 · PROJECTS: COMMAND-LINE TOOLS

head in C#: First 10 Lines with GetRange and Math.Min

Practice Read the full explanation

Build a tiny version of the Unix head command. Given a list of lines, return the first 10 lines joined by newline characters (\n). If the input has fewer than 10 lines, return all of them. If there are no lines at all, return the empty string.

Function signature:

public string Head(List<string> lines)

Example:

Input: lines = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"]

Output: "a\nb\nc\nd\ne\nf\ng\nh\ni\nj"

When printed:
a
b
c
d
e
f
g
h
i
j

Use List.GetRange(int index, int count) to grab the first 10 lines, but cap count at lines.Count when the list is shorter than 10. Then string.Join("\n", subList).

Compute int end = Math.Min(10, lines.Count), then call lines.GetRange(0, end).

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

STEP 1 OF 2
    Ctrl + Enter to check

    C# · PROJECTS: COMMAND-LINE TOOLS

    Head V1 in C#, explained

    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

    1. Compute end as Math.Min(10, lines.Count). It is the number of lines to keep. Math.Min returns the smaller of its two arguments.
    2. Return the joined range: string.Join("\n", lines.GetRange(0, end)). GetRange(0, end) is the first end items, and Join inserts the newline between each two of them. Since end never exceeds lines.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 GetRange is a count, not an end index. The variable is called end here because, starting at index 0, the count and the end position are the same number. With a start other than 0, they would differ.
    • GetRange returns a new list. It copies the references of the selected items into a separate List<string>. Changing the new list does not change lines.
    • Out-of-range ranges throw. Asking for more items than exist throws an ArgumentException, and a negative count throws ArgumentOutOfRangeException. C# doesn't clamp for you the way slicing does in some other languages, which is why Math.Min is needed.
    • Math needs using System; in a file of your own, and List<string> needs using System.Collections.Generic;.
    • Count, not Length. A List<T> reports its size with the Count property.

    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. The Math.Min avoids 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. end is 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.Join doesn'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 "".
    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