Stage 6 · Crucible Crown
Comprehensions
“Can you read a whole forge in one line, little ember?”Crucible King
Scrolls on the trail
These are the 5 lore scrolls you'll find in Crucible Crown, in the order the lesson teaches them. The comment after each print shows what it outputs.
Lore scroll · Comprehensions
Lists in One Line
squares = [n * n for n in range(4)]
print(squares) # [0, 1, 4, 9][expr for n in seq] builds a list: one expr for every n, in order.
Lore scroll · Comprehensions
The Sieve
odds = [n for n in range(8) if n % 2 == 1]
print(odds) # [1, 3, 5, 7]An if at the end keeps only the items it says True for.
Lore scroll · Comprehensions
Words In, Words Out
ws = ["ash", "ember"]
big = [w.upper() for w in ws]
print(big) # ['ASH', 'EMBER']
print([len(w) for w in ws]) # [3, 5]Any expression works on the left: a method call, len(), an index, a sum.
Lore scroll · Comprehensions
Dicts Too
heat = {w: len(w) for w in ["ash", "clay"]}
print(heat) # {'ash': 3, 'clay': 4}Curly braces and key: value build a dict instead of a list.
Lore scroll · Comprehensions
Still a List
nums = [n * 2 for n in range(4)]
print(nums) # [0, 2, 4, 6]
print(sum(nums)) # 12
print(max(nums)) # 6A comprehension is still a list, so sum, len and max all work on it.
The stage puzzle: The comprehension stair
The crown wall is eight tiles of sheer stone. Three forge blocks, A, B and C, each run a list comprehension. Bumping one builds a stair with one plank step for each number in its list, placed that many tiles high. A 0 builds nothing. Only one list gives you a stair you can climb.
A: [n for n in range(3)]
B: [n * 2 for n in range(1, 5)]
C: [n * 4 for n in range(3) if n != 1]
steps = ? # up the crownShow the answer
B builds [2, 4, 6, 8]: even steps, two tiles apart, all the way to the top.
Practice the Crucible King's trial
In the game, the Crucible King duels you in a sealed arena. His question shows across the top of the screen, and you answer by jumping up into the lettered block, A to D, while you dodge his spells. You need 5 right answers to banish him. These are his questions, drawn from all 13 in his bank. The hints work the same as in the game: ask once for his riddle, and ask again for him to rule out a wrong answer.