← Back to course
Code Crew G5 / Week 12 / Thursday
4/6
Week 12 Β· Ship it

Thursday

Crew and restock
// Build, test and graduate
⏱ about 15 min

Thursday: Crew and restock

The supply officer asks two more things. How many crew share the supplies? And can we add a new supply when a cargo ship arrives?

Rocket grins. "Counting the crew is a tree job. I know that one!"

Raven asks, "What do you notice about adding a supply we have never had before?"

"Its key is not in the dictionary yet," Rocket says. "So I cannot add to it."

Nova hums. "Here is a hint. Check if the key is there first."

Part 5: crew and restock

# Part 5: crew and restock
def count_crew(node):
    total = 1
    for child in node["crew"]:
        total = total + count_crew(____)
    return total

def restock(stock, item, amount):
    if item in stock:
        stock[item] = stock[item] + amount
    else:
        stock[item] = ____
First blank: count the smaller tree of
Second blank: a brand new supply starts at

Test drive

def count_crew(node):
    total = 1
    for child in node["crew"]:
        total = total + count_crew(child)
    return total

def restock(stock, item, amount):
    if item in stock:
        stock[item] = stock[item] + amount
    else:
        stock[item] = amount

team = {"name": "Bo", "crew": [{"name": "Cy", "crew": []}]}
print(count_crew(team))
stock = {"rope": 2}
restock(stock, "rope", 10)
restock(stock, "kites", 4)
print(stock)
Output:
2
{'rope': 12, 'kites': 4}
USE THE PART 1 DATA.
  • Read the question.
  • Tap your answer.
What does count_crew(fleet) return?
After restock(inventory, "lamps", 5), what is inventory["lamps"]?
StatementTrue or false?
restock changes the dictionary it is given.?
restock crashes for a supply that is not in the dictionary yet.?
count_crew uses recursion.?
WHY THIS EXERCISEChecking with in first lets restock handle new supplies and old ones.
For a grown-up
Ask your child to swap driver and navigator roles today. The navigator explains each blank before the driver fills it.

Practice lab: restock twice

def count_crew(node):
    total = 1
    for child in node["crew"]:
        total = total + count_crew(child)
    return total

def restock(stock, item, amount):
    if item in stock:
        stock[item] = stock[item] + amount
    else:
        stock[item] = amount

team = {"name": "Eve", "crew": [
    {"name": "Fen", "crew": []},
    {"name": "Gil", "crew": []},
    {"name": "Hal", "crew": []},
]}
print(count_crew(team))
stock = {"maps": 7}
restock(stock, "maps", 3)
restock(stock, "glue", 6)
restock(stock, "glue", 1)
print(stock)
What does the first print line show?
What is the count for maps at the end?
What is the count for glue at the end?
How many keys does stock have at the end?
RESTOCK CHECK
  • Read the question.
  • Tap your answer.
For the first glue call, which part of restock runs?
For the second glue call, which part runs?
What does count_crew return for a leaf?
StatementTrue or false?
count_crew(team) visits four nodes.?
restock(stock, "glue", 6) crashes because glue is new.?
restock changes the stock dictionary.?
in checks whether a key is in a dictionary.?
WHY THIS EXERCISEChecking with in first lets restock handle new and old supplies.
Using Part 1, after restock(inventory, "rope", 10), what is inventory["rope"]?
WHY THIS EXERCISErope is already a key, so 10 is added to 2.

Part 5 done! Every helper is built and test driven. Tomorrow we add the tests and the report, and run the whole thing.

← Wednesday