EasyNo. 0195

Tenth Line — LeetCode 195 Python Solution

Topics
Shell
Reading time
2 min

The problem

Given a text file file.txt, print just the 10th line of the file.

Example

Example
Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Python solution

PythonSolution
def print_tenth_line(path: str = "file.txt") -> None:
    with open(path, "r", encoding="utf-8") as f:
        for i, line in enumerate(f, 1):
            if i == 10:
                print(line.rstrip("\n"))
                break

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Related problems
No.ProblemDifficulty
0194Transpose FileMedium
0193Valid Phone NumbersEasy
0192Word FrequencyMedium
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy

Frequently asked questions

How hard is LeetCode 195. Tenth Line?

LeetCode 195. Tenth Line is rated Easy on LeetCode.

What topics does LeetCode 195. Tenth Line cover?

LeetCode 195. Tenth Line is tagged Shell on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.