S3Q2 · Reorder the Jumbled Lines¶
⚡ Quick Reference
Type: File-in, stdout-out
Core idea: read all lines, parse the last line as new_pos-original_pos pairs, reorder and print.
with open(filename) as f:
lines = f.readlines()
content_lines = lines[:-1] # all lines except the last
order_line = lines[-1].strip() # "1-3,2-4,3-2,4-1"
# Parse pairs and sort by new position
pairs = [pair.split("-") for pair in order_line.split(",")]
ordered = sorted(pairs, key=lambda p: int(p[0]))
for _, orig in ordered:
print(content_lines[int(orig) - 1], end="")
Key rules:
- Last line format: new_pos-original_pos pairs, comma-separated
- Sort pairs by new_pos to get the correct output order
- Lines are 1-indexed - subtract 1 for list indexing
Problem Statement¶
Problem (File I/O → stdout)
Read a file whose last line contains reorder instructions in new_pos-original_pos format. Print the preceding lines in the specified order.
Example:
first line
second line
third line
fourth line
1-3,2-4,3-2,4-1
third line
fourth line
second line
first line
Understanding the order line¶
1-3,2-4,3-2,4-1 breaks down as:
| Pair | New position | Original line |
|---|---|---|
1-3 |
1 | line 3 → "third line" |
2-4 |
2 | line 4 → "fourth line" |
3-2 |
3 | line 2 → "second line" |
4-1 |
4 | line 1 → "first line" |
Output order follows new positions 1 → 2 → 3 → 4:
third line, fourth line, second line, first line ✓
Tracing the example¶
lines = ["first line\n", "second line\n", "third line\n",
"fourth line\n", "1-3,2-4,3-2,4-1"]
content_lines = lines[:-1] # first 4 lines
order_line = "1-3,2-4,3-2,4-1"
pairs = [["1","3"], ["2","4"], ["3","2"], ["4","1"]]
# sorted by new_pos (already in order here)
For each (new, orig):
orig=3 → content_lines[2] = "third line\n" → print
orig=4 → content_lines[3] = "fourth line\n" → print
orig=2 → content_lines[1] = "second line\n" → print
orig=1 → content_lines[0] = "first line\n" → print
Solution approaches¶
with open(filename) as f:
lines = f.readlines()
# Separate content lines from the order instruction
content_lines = lines[:-1]
order_line = lines[-1].strip()
# Parse "1-3,2-4,3-2,4-1" into (new_pos, orig_pos) pairs
pairs = []
for part in order_line.split(","):
new_pos, orig_pos = part.split("-")
pairs.append((int(new_pos), int(orig_pos)))
# Sort by new position to get output order
pairs.sort(key=lambda p: p[0])
# Print each line in the new order
for new_pos, orig_pos in pairs:
line = content_lines[orig_pos - 1] # 1-indexed → 0-indexed
print(line, end="")
with open(filename) as f:
lines = f.readlines()
content_lines = lines[:-1]
order_line = lines[-1].strip()
# Build a mapping: new_position → original_line_content
mapping = {}
for part in order_line.split(","):
new, orig = map(int, part.split("-"))
mapping[new] = content_lines[orig - 1]
# Print in new position order 1, 2, 3, ...
for key in sorted(mapping):
print(mapping[key], end="")
Stores each line keyed by its new position, then iterates sorted(mapping) for the correct output order.
with open(filename) as f:
lines = f.readlines()
content_lines = lines[:-1]
parse = lambda p: tuple(map(int, p.split("-")))
pairs = sorted(map(parse, lines[-1].strip().split(",")))
for _, orig in pairs:
print(content_lines[orig - 1], end="")
Lambda parses each "new-orig" pair into an integer tuple. sorted() on tuples sorts by first element (new position) naturally.
Key takeaways¶
lines[:-1] and lines[-1] split cleanly
lines[:-1] gives all content lines; lines[-1] gives the order instruction. Works regardless of how many content lines there are - no hardcoded indices.
Sort by new position before printing
The pairs in the order line may not appear in new-position order. Always sort by new_pos before iterating - otherwise the output order would follow the order line's listing, not the intended sequence.
1-indexed to 0-indexed: subtract 1
The instruction uses 1-based line numbers; Python lists are 0-indexed. content_lines[orig - 1] converts correctly. Forgetting this offset is a common off-by-one error.