Tenth Line — LeetCode 195 Python Solution

EasyShell
Problem
#195
Reading time
2 min

The problem

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

Example

Line 1

Line 2

Line 3

Line 4

Line 5

Line 6

Line 7

Line 8

Line 9

Line 10

Python solution

Python
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

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

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.

Get Stealth Interview