S3Q2 · Fill the First n Blanks with 1 to n¶
⚡ Quick Reference
Type: File-in, stdout-out
Core idea: read n from the first line, then replace each _ in order with 1, 2, ..., n; leave remaining _ unchanged.
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
lines = f.readlines()
n = int(lines[0].strip())
print(lines[0].strip()) # print n as first line
counter = 1
for line in lines[1:]:
result = []
for ch in line:
if ch == '_' and counter <= n:
result.append(str(counter))
counter += 1
else:
result.append(ch)
print("".join(result), end="")
Key rules:
- Print the first line (n) unchanged
- Replace _ sequentially with 1, 2, ..., n across all remaining lines
- After n replacements, leave remaining _ as-is
- Preserve all other characters and newlines exactly
Problem Statement¶
Problem (File I/O → stdout)
Read n from the first line. Replace the first n underscores in the subsequent text with 1 through n sequentially. Print all lines including the first.
Example:
11
a_bc__d___e
f_g__h__i__j
k___l_m__n
11
a1bc23d456e
f7g89h1011i__j
k___l_m__n
Tracing the example¶
n = 11, counter starts at 1.
Line 2: a_bc__d___e
| Char | _? |
Counter ≤ 11? | Output |
|---|---|---|---|
a |
❌ | - | a |
_ |
✅ | ✅ (1) | 1, counter→2 |
b,c |
❌ | - | bc |
_ |
✅ | ✅ (2) | 2, counter→3 |
_ |
✅ | ✅ (3) | 3, counter→4 |
d |
❌ | - | d |
_,_,_ |
✅×3 | ✅ (4,5,6) | 456, counter→7 |
e |
❌ | - | e |
→ a1bc23d456e ✓
Line 3: f_g__h__i__j (counter=7)
_ → 7, _ → 8, _ → 9, _ → 10, _ → 11, remaining __ → unchanged
→ f7g89h1011i__j ✓
Line 4: k___l_m__n (counter=12 > 11 → all _ unchanged)
→ k___l_m__n ✓
Solution approaches¶
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
lines = f.readlines()
n = int(lines[0].strip())
print(lines[0].strip())
counter = 1
for line in lines[1:]:
result = []
for ch in line:
if ch == '_' and counter <= n:
result.append(str(counter))
counter += 1
else:
result.append(ch)
print("".join(result), end="")
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
lines = f.readlines()
n = int(lines[0].strip())
print(lines[0].strip())
counter = 1
for line in lines[1:]:
while '_' in line and counter <= n:
line = line.replace('_', str(counter), 1)
counter += 1
print(line, end="")
str.replace('_', replacement, 1) replaces only the first _ per call. The while loop repeats until no _ remain or counter exceeds n.
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
content = f.read()
lines = content.split('\n', 1) # split into first line and rest
n = int(lines[0].strip())
rest = lines[1] if len(lines) > 1 else ""
counter = 1
result = []
for ch in rest:
if ch == '_' and counter <= n:
result.append(str(counter))
counter += 1
else:
result.append(ch)
print(n)
print("".join(result), end="")
Key takeaways¶
Counter persists across lines
The replacement counter must not reset between lines - blanks in line 3 continue from where line 2 left off. Initialise it once before the line loop and never reset it.
Check counter ≤ n before replacing
Once the counter exceeds n, remaining _ must be preserved. The condition ch == '_' and counter <= n handles both - extra underscores pass through unchanged.
Multi-digit numbers replace a single underscore
10 and 11 are two-character strings but they replace a single _. Building the result as a list of strings and joining at the end handles variable-width replacements cleanly.