Start learning free

LESSON 607 OF 674 · PROJECTS: COMMAND-LINE TOOLS

CLI Tool: head v1 — First 10 Lines

Practice

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