S3Q2 · String Rearrangement¶
⚡ Quick Reference
Type: File-in, stdout-out
Core idea: split each line on ",", first part is the string, remaining parts are 1-based indices specifying the new character order.
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
for line in f:
line = line.strip()
if not line:
continue
parts = line.split(",")
s = parts[0]
indices = [int(i) - 1 for i in parts[1:]] # convert to 0-based
print("".join(s[i] for i in indices))
Key rules:
- Line format: string,i1,i2,i3,...
- Indices are 1-based → subtract 1 for 0-based access
- Characters are rearranged in the order given by the indices
- Skip blank lines
Problem Statement¶
Problem (File I/O → stdout)
Read a file where each line contains a string followed by comma-separated 1-based indices. Print each string rearranged according to the given index order.
Example:
hello,2,1,3,5,4
abcdef,3,2,1,6,5,4
xyz,3,1,2
ehlol
cbafed
zxy
Tracing all examples¶
Line 1: hello,2,1,3,5,4
s = "hello" (h=0, e=1, l=2, l=3, o=4)
indices = [2,1,3,5,4] → 0-based: [1,0,2,4,3]
s[1]=e, s[0]=h, s[2]=l, s[4]=o, s[3]=l
→ "ehlol" ✓
Line 2: abcdef,3,2,1,6,5,4
s = "abcdef" (a=0, b=1, c=2, d=3, e=4, f=5)
indices → 0-based: [2,1,0,5,4,3]
s[2]=c, s[1]=b, s[0]=a, s[5]=f, s[4]=e, s[3]=d
→ "cbafed" ✓
Line 3: xyz,3,1,2
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:
for line in f:
line = line.strip()
if not line:
continue
parts = line.split(",")
s = parts[0]
indices = [int(i) - 1 for i in parts[1:]]
print("".join(s[i] for i in indices))
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
for line in f:
line = line.strip()
if not line:
continue
parts = line.split(",")
s = parts[0] # the string to rearrange
result = []
for idx_str in parts[1:]:
idx = int(idx_str) - 1 # convert 1-based to 0-based
result.append(s[idx])
print("".join(result))
import tempfile, sys
_, filename = tempfile.mkstemp(prefix="case")
with open(filename, 'w') as f:
f.write(sys.stdin.read())
with open(filename) as f:
for line in f:
line = line.strip()
if not line:
continue
parts = line.split(",")
s = parts[0]
indices = list(map(lambda x: int(x) - 1, parts[1:]))
print("".join(map(lambda i: s[i], indices)))
Key takeaways¶
1-based to 0-based: subtract 1
The indices in the file are 1-based (position 1 = first character). Python strings are 0-based. Always subtract 1 before using the index: int(i) - 1.
parts[0] is the string, parts[1:] are the indices
Splitting on "," gives the string as the first element and index strings as the rest. parts[1:] slices off everything after the string cleanly.
Skip blank lines
Files often end with a trailing newline, creating an empty line. Checking if not line: continue after stripping prevents an error when trying to split an empty string.