Lesson 1 · Unit 04
Choose, explore, undo
One shared path that every call cleans up
What you practise
- The template
- Why you copy the path when you record it
Try a question from this lesson
Pick one
A list can hold a stack of choices. What does this print?
path = []
path.append("a")
path.append("b")
path.pop()
path.append("c")
print("".join(path))const path = [];
path.push("a");
path.push("b");
path.pop();
path.push("c");
console.log(path.join(""));const path: string[] = [];
path.push("a");
path.push("b");
path.pop();
path.push("c");
console.log(path.join(""));Correct answer
ac
Why
The pop removes the most recent choice, b, and leaves the list exactly as it was before b arrived. Then c takes the free place. The a underneath is never touched.
TypeScript noteAn empty [] gives TypeScript nothing to infer from, so the annotation string[] says what the list is going to hold.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
One path, three steps
A recursive call can be handed its own copy of the work, such as a new string. Backtracking instead keeps a single list, the path, and lets every call edit it.
Each choice takes three steps. Choose: add it to the path. Explore: recurse. Undo: remove it again.
After the undo the path looks exactly as it did before the choice, so the next choice starts clean.
def pins(k, path):
if k == 0:
print("".join(path))
return
for d in "12":
path.append(d) # choose
pins(k - 1, path) # explore
path.pop() # undo
pins(2, [])function pins(k, path) {
if (k === 0) {
console.log(path.join(""));
return;
}
for (const d of "12") {
path.push(d); // choose
pins(k - 1, path); // explore
path.pop(); // undo
}
}
pins(2, []);function pins(k: number, path: string[]) {
if (k === 0) {
console.log(path.join(""));
return;
}
for (const d of "12") {
path.push(d); // choose
pins(k - 1, path); // explore
path.pop(); // undo
}
}
pins(2, []);Concept
Record a copy
Saving the path itself saves a reference to the one shared list. The search keeps editing that list, and every undo takes an item out of your saved results too.
So when you record a result, save a copy: a new list with the same items, frozen at that moment.
Printing, or joining into a string, already builds something new. Only saving the list itself needs the copy.
out.append(path) # the shared list
out.append(path[:]) # a frozen copyout.push(path); // the shared list
out.push([...path]); // a frozen copyout.push(path); // the shared list
out.push([...path]); // a frozen copyPython notepath[:] is a slice from the start to the end, which builds a new list. path.copy() and list(path) do the same job.
JavaScript note[...path] spreads the items into a brand new array. path.slice() does the same job.
TypeScript note[...path] spreads the items into a brand new array. path.slice() does the same job.
10 more exercises in the app
- 3× multiple choice
- 2× fill the blanks
- 1× true or false
- 2× type the answer
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.