EasyNo. 0195
Tenth Line — LeetCode 195 Python Solution
- Topics
- Shell
- Reading time
- 2 min
- Source
- leetcode.com
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
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"))
breakComplexity
- Time
- O(n)
- Space · auxiliary
- O(1) to O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0194 | Transpose File | Medium |
| 0193 | Valid Phone Numbers | Easy |
| 0192 | Word Frequency | Medium |
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
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.