Lesson 2 · Unit 03
Every ordering
n choices, then n - 1, then n - 2
What you practise
- N! orderings with a used-set
- Choose, explore, un-choose appears
Try a question from this lesson
Pick one
Three runners finish a race with no ties. How many different finishing orders are possible?
Correct answer
6
Why
Any of the 3 can win, then either of the remaining 2 takes second, and the last place is forced: 3 × 2 × 1 = 6. It is not 2³ = 8, because a runner cannot finish twice.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Pick one, then order the rest
An ordering is a decision per slot: which unused item goes next? Try each one in turn, and let a recursive call fill the remaining slots.
Here cur is the ordering so far, and a letter is unused if it is not in cur yet.
def orders(s, cur):
if len(cur) == len(s):
print(cur)
return
for ch in s:
if ch not in cur:
orders(s, cur + ch)
orders("abc", "")function orders(s, cur) {
if (cur.length === s.length) {
console.log(cur);
return;
}
for (const ch of s) {
if (!cur.includes(ch)) {
orders(s, cur + ch);
}
}
}
orders("abc", "");function orders(s: string, cur: string) {
if (cur.length === s.length) {
console.log(cur);
return;
}
for (const ch of s) {
if (!cur.includes(ch)) {
orders(s, cur + ch);
}
}
}
orders("abc", "");Python notech not in cur scans the string for the character: fine for short strings, O(n) per test.
JavaScript notecur.includes(ch) is the JavaScript spelling of Python's ch in cur.
TypeScript noteNo return type is written here and none is needed: the function is recursive but never uses its own return value.
Concept
Choose, explore, un-choose
Scanning cur for every letter is slow. A shared set of used letters makes the test instant, but shared state has a price: you must undo your choice after exploring it.
The recursive call between the two is the explore step. Without the un-choose, the letter would stay marked as used for every other branch.
def orders(s, cur, used):
if len(cur) == len(s):
print(cur)
return
for ch in s:
if ch in used:
continue
used.add(ch) # choose
orders(s, cur + ch, used)
used.remove(ch) # un-choose
orders("abc", "", set())function orders(s, cur, used) {
if (cur.length === s.length) {
console.log(cur);
return;
}
for (const ch of s) {
if (used.has(ch)) continue;
used.add(ch); // choose
orders(s, cur + ch, used);
used.delete(ch); // un-choose
}
}
orders("abc", "", new Set());function orders(
s: string, cur: string,
used: Set<string>,
): void {
if (cur.length === s.length) {
console.log(cur);
return;
}
for (const ch of s) {
if (used.has(ch)) continue;
used.add(ch); // choose
orders(s, cur + ch, used);
used.delete(ch); // un-choose
}
}
orders("abc", "", new Set());Python noteset() makes an empty set. {} would make an empty dict.
TypeScript noteSet<string> is a set that only holds strings. new Set() is accepted because the parameter type says what it must hold.
8 more exercises in the app
- 3× multiple choice
- 1× order the lines
- 1× true or false
- 1× fill the blanks
- 1× type the answer
- 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.