Computer Science Foundations: study guide
about 15 min to listen5 units25 lessonsevery crash course: about 27 min
The units in study order. For each: what it asks, every rule, every trap, and its crash course.
1 Information in bits
What it asks. Use bits to represent two states, numbers, characters and tiny pictures. Trace how a change in the rules changes what the same bits mean.
Rule Give one bit a meaning
A bit has two possible states, so each mark on paper is either 0 or 1.
A mark has no meaning until the sender and the receiver share a key, and that key must give the two marks two different meanings. Decode one mark at a time, from left to right, and write one word per mark. Keep the marks and change the key, and you get a different message.
Trap Give one bit a meaning
The usual mistake is to treat the mark as if it were the meaning: "1 is ready, full stop." The tile shows only a face. Ready comes from Key A, and under Key B the same face means waiting. A computer keeps physical states, not the digits 0 and 1, so the digit names a state and the key turns that state into a meaning.
The second mistake is to reverse the marks instead of the key. The row 1, 0, 1 reads the same backwards, so flipping its order gives 1, 0, 1 again and repeats Key A's message without anyone noticing. Try 1, 1, 0 under Key A instead: it decodes as ready, ready, waiting.
Reversing the order of 1, 1, 0 gives 0, 1, 1, which Key A decodes as waiting, ready, ready. Reversing the key gives waiting, waiting, ready. These are three different answers, and only the last one answers "reverse the key".
A key that gives both marks the same meaning, such as 0 means ready and 1 means ready, also fails. Every message decodes as ready, and waiting can never be sent.
Rule Read binary place values
This rule models one convention: a four-bit unsigned pattern with exactly four bits, columns worth 8, 4, 2 and 1 from left to right, and no sign.
Other conventions read the leftmost bit as a plus or minus sign, so the answers here hold only when the pattern is stated to be unsigned.
To decode, the initial state is a total of 0 with your pencil at the 8 column. Move left to right, one column at a time. If the column holds 1, add its value to the total; if it holds 0, add nothing. Stop after the 1 column, and the total is the number.
To encode, the initial state is a remaining amount equal to the number, with four empty boxes under the headers 8 4 2 1. Move left to right, starting at the 8 column. If the column's value is no more than the remaining amount, write 1 and subtract the value; otherwise write 0. Stop after the 1 column. If the remaining amount is 0, the four marks are the pattern; if it is not 0, the number does not fit in four bits.
Trap Read binary place values
If you read 1010 as one thousand and ten, you have used decimal columns, which step by ten: 1000, 100, 10 and 1. The same four marks then name a much larger number. Binary columns step by two, so 1010 in binary is 8 + 2 = 10.
Three habits guard against this. Write "in binary" next to the pattern, write the headers 8 4 2 1 above the digits before you read, and check that every digit is 0 or 1. A digit from 2 to 9 means the marks are not a binary pattern at all.
Rule Find what fits in three bits
Start with one empty row and double the count for each bit you add.
Two choices for each of three positions make eight patterns. The unsigned values run from zero to one less than the pattern count. With three bits they run from 0 through 7.
Trap Find what fits in three bits
If you counted seven codes, check where you started. The pattern 000 reads as zero and is one of the eight patterns. If the booth sets 000 aside to mean “no ticket,” it still appears in the list. The booth has seven codes to hand out.
Rule Encode a character as bytes
Capital A has code point U+0041.
Its UTF-8 byte is 41. Lowercase é has code point U+00E9. It uses two UTF-8 bytes, C3 first and A9 second.
A base letter followed by a separate combining accent can use two code points, one for the letter and one for the accent. Together they form a combining sequence. Use the single code point U+00E9 for each é in this lesson's inputs.
Trap Encode a character as bytes
A character count alone leaves the UTF-8 byte count open. AA and Aé each have two letters here. AA takes two bytes, and Aé takes three. Work out how many bytes each code point adds before counting the total.
Rule Decode a tiny bitmap
The paper format is the set of rules that tells you how to read the bits.
It gives you the width, height, colour key and reading order. Both dimensions are whole numbers of at least one. Multiply the width by the height to count the squares. You need exactly that many bits, including every 0.
For the two grids shown here, the key is 1 for ink and 0 for white paper. Start with an all-white grid and the first bit. Work across each row from left to right, starting with the top row. Stop when you use the last bit in the bottom-right square.
Trap Decode a tiny bitmap
Use the width to decide where to end each row. Grid A needs three bits across each of its two rows. If you use the height of two as the row length and lay out all six bits, the third bit starts a new row and you get Grid B. Check the key as well. If you swap what 0 and 1 mean, every ink square becomes white and every white square becomes ink.
2 Reason with true and false
What it asks. Write true-or-false questions about everyday situations. Use a truth table to find when two rules give different answers.
Rule Read a two-state predicate
This paper model uses one or more labeled slots, each with a known state.
For each slot, record the Boolean answer to “Is this slot free?” Use true for free and false for occupied.
Use the states from one moment and keep them fixed while you work. The rule here covers a fixed set of slots with no missing states. For the whole set, give true if one or more slots are free. Give false if zero slots are free.
Trap Read a two-state predicate
On the returns cart, the count 1 answers “How many slots are free?” That one free slot makes the answer to “Is at least one slot free?” true. Keep the free count and the predicate result on separate lines.
Rule Fill an AND table
A AND B gives true only when A and B are both true.
If either input is false, the result is false.
Trap Fill an AND table
Suppose a visitor has a receipt and gives a name that differs from the tag. Only A gets a tick. That row gives false, so the piece stays at the desk.
Rule Fill an OR table
For two Boolean inputs, A OR B gives true if at least one input is true.
It gives false if both are false.
Assume both input values are known and refer to the same light. The paper model answers whether the resident made a report. Delivery, reading the report, and repairing the light are outside the model.
Trap Fill an OR table
Suppose the resident phones and then emails. Does the email cancel the phone report? The phone report still counts after the email, so keep Reported true in the marked last row, where both inputs are true.
Rule Negate a condition
The input to NOT is the known value you start with, here the value of A.
In this paper model it can be either of the two Boolean values, false or true. Write those values in lowercase and use uppercase NOT to show that you’re reversing a value. The output is the value you get from the rule. For false, write true. For true, write false.
Trap Negate a condition
Leave the sign alone and work with the value on your paper. Turning the sign would give you a different observation. If the face is covered, stop before writing either value. Find out which face was showing at noon before you choose a row.
Rule Check two rules against every input pair
Compare the two final rule answers on the same row.
If every row matches, the rules are equivalent. If any row differs, record that input pair as a counterexample.
Trap Check two rules against every input pair
Two matches can look like proof that the rules are equivalent. If you check only false/false and true/true, you miss the mixed rows, where one input is true and the other is false. Each mixed row makes the rules disagree.
3 Algorithms on paper
What it asks. Write a procedure as numbered steps with a named input and output. Trace its decisions and its repetition by hand, then test it with inputs chosen to show a mistake.
Rule State inputs and outputs
A problem statement names the input, says which inputs are allowed and names the output each allowed input needs.
An algorithm solves the problem when it ends with the correct output for every allowed input.
Trap State inputs and outputs
The common slip is writing steps before the limits are settled. A statement that says a row of cards and stops there leaves the empty row open. One reader decides the output is 0, and another decides there is none. Decide which inputs are allowed and write the decision into the statement. In this unit the empty row is not allowed.
A second slip is naming the wrong output. The largest number in 5 5 3 is 5. The position of the largest card is a different output, and for this row two positions hold it.
Rule Write an unambiguous instruction
Each step of an algorithm has exactly one reading.
The steps are carried out in the order written unless a step says where to go next. Two readers who follow the same steps on the same input do the same things and reach the same output.
Trap Write an unambiguous instruction
The usual gap is a case nobody wrote down. Suppose step 2 said only this: take the next card. On the row 4 9 2 a reader reaches the end of the row with no instruction for what comes next. One reader stops, and another starts again from the first card. Write the instruction for the case where no card is left.
A second question sits in the word larger. If step 3 acted for a card that is larger than or equal to the noted number, the row 5 5 3 would still give 5, and the reader would rewrite the slip one more time on the way. Each wording has one reading. The step must say which of the two it means.
Rule Trace a decision
At a selection step, work out the condition with the current values each time the step is reached.
If it is true, carry out the action the step gives for true. If it is false, carry out the step's otherwise action when it has one. After either, follow any instruction about where to go next, and continue with the next step in order when there is none. The condition decides which action is carried out.
Trap Trace a decision
Do not skip a step because the answer seems clear. Carry out every step the procedure reaches, including the ones whose condition is false.
The opposite slip is carrying out an action whose condition was false, or going on to step 3 after step 2 has said go to step 5. For 0 days late that slip announces 1 coin where the rule gives 0. Sequencing means carrying out the steps in the order the procedure gives them. Follow each jump as written.
Rule Explain when a repetition ends
A loop repeats its steps until its stopping condition is met.
If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.
Trap Explain when a repetition ends
A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.
The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes 1, then repeats two steps: add 2 to the number, and stop if the number is 10. The number goes 3, 5, 7, 9, 11 and on, and never equals 10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes.
Rule Check a procedure against a counterexample
A test case is an allowed input together with the output expected for it.
In this unit the expected output is worked out by hand before the procedure is carried out. A procedure passes a test when it announces that output. An allowed input for which it announces anything else is a counterexample, and one counterexample shows that the procedure does not solve the problem. Passing tests cover only the inputs tried.
Trap Check a procedure against a counterexample
After several passing tests it is tempting to call the procedure correct. The three rows that pass all hold a number above 0, which hides the mistake. The changed procedure fails only on rows whose numbers are all below 0. Choose tests that differ from each other in kind, including inputs at the extremes of what is allowed.
A second slip is patching the counterexample alone, for instance with a new step that announces -3 for that one row. The row -9 -4 would then fail in the same way. Correct the steps that cause the error by restoring the first two steps of the largest-card procedure of lesson 2. Step 1 takes the first card and writes its number on the slip, and step 2 goes on with the cards that are left.
4 Search, sort and cost
What it asks. Search a row of cards one by one and by halves, put cards in order two ways, and count the comparisons each procedure makes as the row grows.
Rule Count comparisons in a linear search
A linear search checks the cards in order and stops at the first card that shows the target, or after the last card.
It makes one comparison for each card it checks: one when the target is first, and as many as there are cards when its first match is the last card or it is not in the row.
Trap Count comparisons in a linear search
A linear search that stops early cannot say not found. If you check three cards of 7 3 9 4 for the target 4 and stop, the 4 on the last card is missed. A linear search says not found only after the last card.
When the target appears twice, the search stops at the first one. In the row 6 2 8 2 5, a search for 2 stops at position 2 after two comparisons and never reaches the second 2.
Rule Keep a sorted interval for binary search
Binary search works only on a sorted row.
Each check compares the target with the middle card of the interval: a match ends the search, and otherwise the interval keeps only the half that can still hold the target. When the interval is empty, the target is not in the row.
Trap Keep a sorted interval for binary search
Binary search on a row that is not sorted can miss a target that is there. In the row 9 2 7 4 5, a search for 4 reads the middle card 7, keeps positions 1 to 2 because 4 is smaller, reads the 9, and ends with not found, though the 4 sits at position 4.
Rule Insert one card into sorted order
With this lesson's steps, a new card goes into a sorted row like this: compare it with the cards from the right-hand end and place it directly to the right of the first card that is not larger, or first in the row when every card is larger.
The row stays sorted. Insertion sort, as the Field Guide describes it, inserts each card in turn into the sorted row so far.
Trap Insert one card into sorted order
The case with no stopping card needs its own step. Inserting 2 into 4 9 13 passes 13, 9 and 4, all larger, and reaches the left-hand end with no card that is not larger. The step for that case places the new card first, giving 2 4 9 13.
A second slip is placing the new card to the left of the stopping card. Inserting 12 next to 11 that way gives 3 8 12 11 15 20, which is not sorted. The new card goes to the right of the first card that is not larger.
Rule Trace a sort on repeated keys
Selection sort moves the smallest unsorted card to the sorted row, pass after pass, until no unsorted card is left, and finding the smallest of n unsorted cards takes n - 1 comparisons.
With this lesson's rules, a card replaces the smallest so far only if its key is smaller, and the cards left unsorted keep their order; then the leftmost of equal smallest cards moves first, and equal keys keep their order.
Trap Trace a sort on repeated keys
One word changes the order of equal keys. If the mark switched to a card that is smaller or equal, pass 3 would move the blue 4 first, and the sorted row would end blue 4, red 4. Both rows are sorted by key, but only one keeps the start order of the fours. Write the test you mean.
A second slip is comparing against cards already sorted. Each pass compares only unsorted cards, which is why the count falls by one each pass.
Rule Compare work as the collection doubles
To compare the efficiency of two procedures, count a step, such as a comparison, as a function of the input size.
When a row doubles, a linear search's worst case doubles, a binary search's grows by one check, and, from 4 cards on, selection sort's count grows a little more than four times.
- College Board, AP Computer Science Principles Course and Exam Description
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.2 Searching
- University of Canterbury Computer Science Education Research Group, Computer Science Field Guide: Algorithms, 2.3 Sorting
Trap Compare work as the collection doubles
Comparing one small input hides the difference. On 3 cards the two searches make at most 3 and at most 2 comparisons, but on 63 cards they make at most 63 and at most 6. Compare how the counts grow.
A best case can also mislead. A linear search whose target sits on the first card makes one comparison at any size; the worst case, a target absent from the row or first found on the last card, shows the cost of a big row.
5 Networks
What it asks. Send a message as numbered pieces on paper, the way data crosses the internet in packets. Put pieces back in order, notice a missing one, acknowledge what arrived and ignore a copy that arrives twice.
Rule Agree on a message format
A protocol is an agreed set of rules.
In this unit's card protocol every data card holds, in order, To, From, Piece (a number of a total) and Data (from 1 to 4 characters); a card that breaks the format is not used.
Trap Agree on a message format
A card that leaves out the total, Piece 2 instead of Piece 2 of 3, breaks the protocol, and the receiver cannot learn the total from that card. Keep both numbers.
A format that only one side knows is not a protocol. If Ana changes the data length to 5 characters without telling Kai, Kai's checks reject her cards.
Rule Split a message into numbered pieces
To send a message with the card protocol, cut it into pieces of 4 characters from the left, the last piece holding the 1 to 4 characters left over, and number the pieces from 1 to N, where N is the length divided by 4 and rounded up.
Every card carries To, From, its number of N and its data.
Trap Split a message into numbered pieces
Leaving spaces and punctuation out of the count can give the wrong number of pieces: MEET-AT-NOON. counted without its period has 12 characters and gets 3 cards instead of 4. Every character is part of the message.
Numbering from 0 on some cards and from 1 on others breaks the agreement. The card protocol numbers from 1 to N.
Rule Reassemble pieces arriving out of order
The receiver places each usable card in the slot of its piece number when that slot is empty, whatever the order of arrival, and reads the data of slots 1 to N in order once all N slots are filled; an empty slot means no usable card for that piece has arrived yet.
Trap Reassemble pieces arriving out of order
Joining pieces in the order they arrive gives NOONMEET-AT- for the arrivals 3, 1, 2. Place by number.
Reading the message while a slot is empty gives a message with a hole in it. Wait for every slot; the next lesson shows how the sender sends a missing piece again.
Rule Use acknowledgments in a paper protocol
In the card protocol, which carries one message at a time, the receiver sends ACK n for each usable piece n, and the sender keeps a list of pieces not yet acknowledged, removes a piece when its acknowledgment arrives, and sends a piece again when its time runs out.
The sender knows every piece has arrived when every piece has been sent and its list is empty; the receiver may hold them all sooner.
Trap Use acknowledgments in a paper protocol
Waiting forever for an answer stalls the whole message. Without a timeout, a lost piece is never sent again.
Acknowledging a card before checking it defeats the purpose. An acknowledgment says the piece arrived and is usable; a card that fails the format checks is not acknowledged. The checks cannot catch every change: NOON turned into SOON on the way still passes them.
Rule Explain a duplicate request
When a lost acknowledgment leaves a piece on the sender's list as its wait runs out, the sender sends it again; if that copy arrives, the receiver gets a duplicate.
In the card protocol the receiver recognizes a duplicate because the slot of its number is already filled, keeps the copy it has, and sends the acknowledgment again.
Trap Explain a duplicate request
Treating a duplicate as a new piece breaks the message. Appending the second -AT- would give MEET-AT-NOON-AT-. Place by number, and a filled slot stays as it is.
Ignoring a duplicate without acknowledging it again can leave the sender waiting: if no acknowledgment reaches it, its time runs out again and it keeps resending. Answer every usable copy.