# Integrated Programming Synthesis

Computer Science I · Programming II · https://tryals.app/learn/computer-science-i/integrated-programming-synthesis

A capable programmer does not apply concepts in isolation; they synthesize them to build real systems. Consider a word-frequency counter reading a text to report the top ten words.

| Phase / Concept | Role in Synthesis |
| --- | --- |
| **Design** | Split into cohesive functions: read, split, count, sort, report. |
| **Data Structures** | Use a **dictionary** for word counts and a **sorting or heap** mechanism for ranking. |
| **Complexity** | Counting is $O(n)$; a naive maximum scan is $O(n^2)$, while sorting distinct words $m$ is $O(m \log m)$. |

**State and purity** is another key axis: the counting dictionary is mutable state that must be deliberately contained, while word-splitting and formatting can remain pure, highly testable functions.

## Defensive Practice and Craft

Synthesis requires making a program robust against unexpected conditions. You must handle empty files, messy punctuation, and case-folding where "The" equals "the".

| Test Type | Target Component |
| --- | --- |
| **Unit Tests** | Word splitter and frequency counter. |
| **Integration Test** | The complete end-to-end pipeline. |
| **Edge-Case Tests** | Empty input and identical frequency ties in the top ten. |

**Common pitfall:** Treating each concept as an isolated exam topic rather than a tool to combine. Real problems demand the right data structure for efficiency, contained state for clarity, and tests for trust.

## Practice questions

5 of this lesson's 10 practice questions, with answers. The full set is in the app.

### 1. A word-frequency counter needs to split text, normalise case, count occurrences and report the top results. Why is choosing a dictionary for the counting step the decision that shapes the whole program?

A. Because dictionaries automatically sort the results, which the reporting step needs.
B. Because the other three steps cannot be written without a dictionary.
C. Because every word needs a lookup, and a dictionary makes that O(1).
D. Because a dictionary is the only structure that can associate a word with a number.

**Answer:** C. Because every word needs a lookup, and a dictionary makes that O(1).

**Why:** The counting step performs one lookup per word, so its cost is multiplied by the length of the text. O(n) with a dictionary and O(n²) with a list of pairs, fine on a paragraph and hopeless on a book. Splitting and normalising have no such leverage. Dictionaries are not sorted either, so reporting still needs a sort, cheap, because it runs once rather than n times.

Page: https://tryals.app/practice/computer-science-i/integrated-programming-synthesis/a-word-frequency-counter-needs-to-split-text-normalise-case-count

### 2. Order the tasks of building the word-frequency counter so that each one can be tested as soon as it is written.

**Answer:**

1. Write and test a function that splits a string into lower-case words
2. Write and test a function that turns a list of words into a word-to-count dictionary
3. Write and test a function that returns the ten highest counts from that dictionary
4. Wire the three functions together into one pipeline
5. Write an integration test that runs the pipeline on a small file with a known answer

**Why:** Building along the direction of data flow means every function can be unit-tested the moment it is written, using real output from the stage before. The integration test comes last because it exercises the interfaces, which only exist once the pieces are wired together.

Page: https://tryals.app/practice/computer-science-i/integrated-programming-synthesis/order-the-tasks-of-building-the-word-frequency-counter-so-that-each

### 3. A word-frequency program is asked to handle a 50 GB file that will not fit in memory. Which part of the design has to change?

A. The assumption that the whole input is available at once
B. The counting logic, which becomes incorrect at scale
C. The dictionary, which must be replaced with a list for large inputs
D. Nothing, the program will simply run more slowly

**Answer:** A. The assumption that the whole input is available at once

**Why:** Distinct words grow far more slowly than text, so the counter fits comfortably while the file does not, process a line at a time and the memory ceiling disappears. Recognising which structure must scale with input and which need not is the whole of the change, and it generalises to most data processing.

Page: https://tryals.app/practice/computer-science-i/integrated-programming-synthesis/a-word-frequency-program-is-asked-to-handle-a-50-gb-file-that-will

### 4. Sort each part of the word-frequency program by whether it can be written as a pure function or must carry mutable state.

**Answer:**

- Can be pure: Splitting a string into a list of lower-case words, Formatting a finished count table as text for display, Selecting the ten largest counts from a finished dictionary
- Must carry mutable state: The dictionary that accumulates counts as words arrive, The running tally updated once per word of the text

**Why:** Splitting, formatting and selecting all take a finished input and return a finished answer, so they can be pure and trivially tested. Accumulation is inherently stateful, which is why the advice is not to eliminate mutation but to contain it in the one place that genuinely needs it, keeping the rest pure.

Page: https://tryals.app/practice/computer-science-i/integrated-programming-synthesis/sort-each-part-of-the-word-frequency-program-by-whether-it-can-be

### 5. Select every edge case the lesson says the word-frequency counter's tests should cover.

A. An empty input file
B. Punctuation attached to words
C. Ties among the top ten counts
D. Words differing only in capitalisation
E. A file larger than the machine's disk

**Answer:** A. An empty input file; B. Punctuation attached to words; C. Ties among the top ten counts; D. Words differing only in capitalisation

**Why:** Empty input, punctuation, case-folding and ties are all points where the program must make a choice, and a test pins that choice down. A file bigger than the disk cannot exist to be read, so it is not a behaviour the program can define. Edge cases are decisions, not disasters.

Page: https://tryals.app/practice/computer-science-i/integrated-programming-synthesis/select-every-edge-case-the-lesson-says-the-word-frequency-counters
