MediumNo. 0194
Transpose File — LeetCode 194 Python Solution
- Topics
- Shell
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given a text file file.txt, transpose its content. You may assume that each row has the same number of columns, and each field is separated by the ' ' character.
Example
name age alice 21 ryan 30
Python solution
def transpose_file(path: str = "file.txt") -> None:
with open(path, "r", encoding="utf-8") as f:
rows = [line.strip().split() for line in f]
if not rows:
return
cols = len(rows[0])
for c in range(cols):
print(" ".join(row[c] for row in rows))Complexity
- Time
- O(n)
- Space · auxiliary
- O(1) to O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0193 | Valid Phone Numbers | Easy |
| 0195 | Tenth Line | 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 194. Transpose File?
LeetCode 194. Transpose File is rated Medium on LeetCode.
- What topics does LeetCode 194. Transpose File cover?
LeetCode 194. Transpose File is tagged Shell on LeetCode.