← Back to course
Code Crew G5 / Week 11 / Tuesday
2/6
Week 11 Β· Plan your capstone

Tuesday

Break it into parts
// Choose, break it down, test it first
⏱ about 15 min

Tuesday: Break it into parts

Rocket opens a blank file for the inventory tracker. He stares at it. "Where do I even start?"

Raven draws a box labeled Tracker. Under it she draws smaller boxes. "What do you notice about my drawing?"

"It is a tree!" Rocket says. "The whole job is the root, and each part is a child."

"And each part is small enough to build in one sitting," Raven says.

Nova hums. "Here is a hint. Give each small part its own function."

Decompose the tracker

To decompose a problem means to break it into smaller parts. Each part becomes one function with one job.

PartJobFunction
CountAdd up every supplytotal_items(stock)
LowList the supplies below a limit, in A to Z orderlow_stock(stock, limit)
FindFind an item in a sorted list of names, fastfind_item(names, target)
CrewCount everyone in the command treecount_crew(node)
RestockAdd more of a supplyrestock(stock, item, amount)
ReportPrint everything clearlyreport(stock, tree)
Tracker
↓
Data: supplies and crew tree
↓
Helpers: total, low, find, crew, restock
↓
Tests
↓
Report
WHICH FUNCTION DOES EACH JOB?
  • Read the question.
  • Tap your answer.
How many supplies are there in all?
We just got 10 more rope.
PUT THE BUILD STEPS IN A SENSIBLE ORDER.
  • ?Run the whole program and check the output
  • ?Write the report that uses the helpers
  • ?Write the helper functions, one at a time
  • ?Write the data: supplies and the crew tree
  • ?Test each helper
WHY THIS EXERCISEBuilding small parts first means each test checks only a little code at a time.
The report calls four helpers: total_items, low_stock, find_item and count_crew. Which helper from the table does it NOT call?
WHY THIS EXERCISErestock changes the data before the report runs, so the report itself does not need it.

Practice lab: decompose the quiz

PartJobFunction
QuestionsStore each question with its answermake_quiz()
AskShow a question and read an answerask_one(question)
CheckCompare an answer with the right oneis_right(given, right)
ScoreAdd up the right answersscore(results)
def is_right(given, right):
    return given == right

def score(results):
    total = 0
    for r in results:
        if r:
            total = total + 1
    return total

print(is_right("8", "8"))
print(is_right("9", "8"))
print(score([True, False, True, True]))
What does the first print line show?
What does the second print line show?
What does the third print line show?
What does score([]) return?
DECOMPOSE CHECK
  • Read the question.
  • Tap your answer.
Which function shows a question to the player?
Which part should be built first?
Why give each part its own function?
StatementTrue or false?
is_right returns True or False.?
score counts the False values.?
The quiz plan has four parts.?
Decomposing turns one big job into a tree of small jobs.?
WHY THIS EXERCISEA clear plan makes the build much easier.
What does score([True, True, False]) return?
WHY THIS EXERCISEtotal goes up once for each True.

Super decomposing! A big job is just small jobs in a tree. Tomorrow we choose the right container for each piece of data.

← Monday