S2Q2 · Update Todo List Based on Given Indices¶
⚡ Quick Reference
Type: Full I/O problem
Core idea: read the todo items, convert the indices to a set, replace [ ] with [x] for matching indices.
n = int(input())
indices = set(map(int, input().split()))
for i in range(n):
line = input()
if i in indices:
line = line.replace("[ ]", "[x]", 1)
print(line)
Key rules:
- First line: n (number of items)
- Second line: space-separated 0-based indices to mark complete
- Out-of-range indices are silently ignored
- Replace - [ ] with - [x] for matched lines
Problem Statement¶
Problem (I/O type)
Read a todo list and a set of indices. Mark the items at those indices as completed (- [x]). Out-of-range indices are ignored.
Example 1:
3
0 2
- [ ] Buy groceries
- [ ] Read a book
- [ ] Write code
- [x] Buy groceries
- [ ] Read a book
- [x] Write code
Example 2:
3
1 3 4
- [ ] Go to the gym
- [ ] Cook dinner
- [ ] Clean the house
- [ ] Go to the gym
- [x] Cook dinner
- [ ] Clean the house
Understanding the problem¶
Two things to handle:
- Which indices to mark -read from line 2, convert to a set for O(1) lookup
- How to mark -replace
[ ]with[x]in the matching lines
Use a set for indices
Storing the indices in a set makes the i in indices check O(1) regardless of how many indices there are. It also automatically ignores duplicates. Out-of-range indices (like 3 and 4 in Example 2 where n=3) are never matched since i only goes from 0 to n-1 -no explicit range check needed.
Tracing Example 2¶
Input indices: {1, 3, 4}, n = 3
| i | Line | i in {1,3,4}? |
Output |
|---|---|---|---|
| 0 | - [ ] Go to the gym |
❌ | - [ ] Go to the gym |
| 1 | - [ ] Cook dinner |
✅ | - [x] Cook dinner |
| 2 | - [ ] Clean the house |
❌ | - [ ] Clean the house |
Indices 3 and 4 are never reached -ignored automatically.
Solution approaches¶
n = int(input())
indices = set(map(int, input().split()))
items = [input() for _ in range(n)]
for i, line in enumerate(items):
if i in indices:
line = line.replace("[ ]", "[x]", 1)
print(line)
Read all items into a list first, then process with enumerate. Slightly more memory but makes the logic very clear.
n = int(input())
indices = set(map(int, input().split()))
for i in range(n):
line = input()
if i in indices:
line = line.replace("[ ]", "[x]", 1)
print(line)
Process each line as it's read -no intermediate list needed. More memory-efficient.
n = int(input())
indices = set(map(int, input().split()))
items = [input() for _ in range(n)]
result = [
line.replace("[ ]", "[x]", 1) if i in indices else line
for i, line in enumerate(items)
]
print("\n".join(result))
Builds the entire output list with a comprehension, then prints in one call. The ternary decides whether to replace or keep each line.
n = int(input())
indices = set(map(int, input().split()))
items = [input() for _ in range(n)]
result = list(map(
lambda pair: pair[1].replace("[ ]", "[x]", 1) if pair[0] in indices else pair[1],
enumerate(items)
))
print("\n".join(result))
map applies a lambda to each (index, line) pair from enumerate. The lambda checks if the index is in the set and replaces accordingly. Pure functional style.
Why replace("[ ]", "[x]", 1)?¶
str.replace(old, new, count) -the third argument 1 limits to only the first occurrence. This is a safety measure: if a todo item's text happened to contain [ ] somewhere, only the checkbox at the start gets replaced.
"- [ ] Buy [ ] milk".replace("[ ]", "[x]", 1)
# → "- [x] Buy [ ] milk" (only first occurrence replaced)
Key takeaways¶
set for O(1) index lookup
Convert indices to a set upfront. Membership check i in indices is O(1) for sets vs O(n) for lists. Out-of-range indices are silently ignored since i never reaches them.
str.replace(old, new, 1) -limit replacements
The third argument to replace() caps how many substitutions are made. Always use 1 when you only want to change the first occurrence -protects against accidental replacements elsewhere in the string.
map(int, input().split()) for integer lists
The standard pattern for reading a line of space-separated integers. input().split() gives string tokens; map(int, ...) converts them all to integers in one pass.