Computer Science Foundations: crash course
about 27 min to listen5 units25 lessons0 questions to try
Short view. Each lesson's rule, its steps and its trap. Switch to Full for a worked example and a question to try in every lesson.Full view. Each lesson's rule, its steps, one worked example with Watch it solved, the trap, and one question to try.
Every unit, and under it every lesson. A unit's own crash course adds the unit's quick checks.
1 Information in bits
1.1 Give one bit a meaning
The rule
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.
How
- Agree on the key with your partner and write it down before anyone sets a tile. In this lesson, Key A says 0 means waiting and 1 means ready.
- To reverse a key, swap its two meanings. Key B, the reversed lift key, says 0 means ready and 1 means waiting. Reversing the key never changes the order of the marks.
- Check the marks. Only 0 and 1 are allowed. If a mark is anything else, or a tile shows no clear face, write "not in the key" and stop instead of guessing.
- Start with an empty message. This is the starting point of your trace.
- Read the marks from left to right, one mark at a time.
- Look up each mark in the key and write its word at the end of the message. In this lesson a mark means the same thing wherever it sits in the row.
- Stop when no marks are left. Count the words: there should be one word for each mark.
- To encode, work the other way: look up each word in the key and write its mark.
- To check, encode your decoded message with the same key. If you get the original marks back, your decoding holds. This check is called a round trip.
The trap
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.
1.2 Read binary place values
The rule
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.
How
- Write the headers 8 4 2 1 across your paper and draw a box under each.
- To decode, copy the pattern into the boxes, one digit per box. Check that there are four digits and that each is 0 or 1.
- Set the total to 0. Working left to right, add the header of every box that holds 1 and skip every box that holds 0.
- Stop after the 1 column. The total is your number.
- To encode, write the number as the remaining amount. Working left to right, ask whether the header is no more than the remaining amount.
- If it is, write 1 in that box and subtract the header from the remaining amount. If it is not, write 0.
- Stop after the 1 column. If the remaining amount is 0, read the four boxes as your pattern and keep any leading zeros. If it is not 0, the number does not fit in four bits.
- Check by running the rule the other way: decode the pattern you wrote, or encode the number you found, and compare with where you started.
The trap
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.
1.3 Find what fits in three bits
The rule
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.
How
- Write down the convention, the rule for using the codes. Give each ticket a different three-bit pattern and make every pattern available.
- Begin with no digits and fill the slots from left to right. Follow
0before1at each split. Record the row once it has three digits, then go back to the last split with a branch left to follow. Finish when you have followed every branch. - Count the finished rows, including
000. Find the largest unsigned value by adding the place values in the all-ones row.
The trap
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.
1.4 Encode a character as bytes
The rule
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.
How
- Use UTF-8. Start with an empty row and a byte count of zero. Write one box per byte and two hex digits per box.
- Read the input from left to right. For each letter, find its code point in The rule. Add that code point's bytes to the right of the row in the order shown.
- Stop once you have written the bytes for the last letter. Read the finished row left to right and count the boxes.
The trap
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.
1.5 Decode a tiny bitmap
The rule
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.
How
- Draw the given number of columns and rows. Leave all squares white. Count the squares and the bits. If the counts differ, stop before shading and report the two counts.
- Start at the top-left square with the first bit. Use the key to decide whether to shade the square or leave it white.
- Use the next bit in the next square to the right. At the end of a row, move to the leftmost square of the next row down if there is one. Stop after you use the last bit in the bottom-right square.
- Read the drawing back from the top left in the same order. Use the key to write one bit per square and join the rows into one line with no spaces.
The trap
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
2.1 Read a two-state predicate
The rule
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.
How
- Write the total number of slots and the number occupied.
- Subtract the occupied count from the total. Give true if the free count is one or more. Give false if it is zero.
- To check, draw one box per slot and start with every box unmarked. Visit the slots once in label order. Mark each occupied box and leave each free box unmarked. Stop after the last label. Count the unmarked boxes and use that count to answer the question again.
The trap
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.
2.2 Fill an AND table
The rule
A AND B gives true only when A and B are both true.
If either input is false, the result is false.
How
- Draw three columns. Label them A (receipt present), B (name matches) and A AND B (result). Make four rows beneath the labels.
- From top to bottom, write
false/false,false/true,true/false,true/true. In each pair, put the first value under A and the second under B. Use onlytrueandfalsefor the inputs. Leave the four result spaces blank. - Start at the top and work down. Tick each input that is
true. Writetruein the result column if the row has two ticks. Writefalseif it has fewer. - Stop when every row has a result.
The trap
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.
2.3 Fill an OR table
The rule
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.
How
- Draw the table on paper with A on the left, B in the middle, and Reported on the right.
- Write the input pairs from top to bottom. Write false and false, false and true, true and false, then true and true. Start with all four Reported spaces blank.
- Start at the top. Read A first. If A is true, write true in Reported. If A is false, read B and copy it into Reported.
- Move down one row at a time. Stop when every Reported space has a value.
The trap
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.
2.4 Negate a condition
The rule
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.
How
- Draw a table on paper with rows for CLOSED and OPEN. Add columns for A and NOT A and leave their cells blank.
- Start with CLOSED. Check whether that row’s face shows OPEN to fill in A. Then use NOT to write the other value beside it.
- Repeat that process for OPEN. Once both rows have a NOT A value, stop filling the table.
- Check by using each value in the NOT A column as the input to the rule one more time. From top to bottom, true gives false and false gives true. Compare false, then true, with the original A column. Both rows match.
The trap
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.
2.5 Check two rules against every input pair
The rule
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.
How
- Write the four input pairs in order. Leave the result and comparison cells blank.
- In the first row, find
A OR B. Flip that result and record it underNOT (A OR B). - In the same row, flip
Ato getNOT Aand flipBto getNOT B. UseORon those two values to fill(NOT A) OR (NOT B). - Compare the answers under
NOT (A OR B)and(NOT A) OR (NOT B). Mark the rowmatchormismatch. - Repeat the calculation and comparison for the remaining rows. Finish the fourth row even if you have already found a counterexample.
The trap
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
3.1 State inputs and outputs
The rule
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.
How
- Write the word Input and say what the algorithm is given. For the card problem, that is a row of cards.
- Add the limits on the input: how many items, and of what kind. Here the limits are one or more cards, each showing an integer.
- Write the word Output and say what must be handed back. Here it is the largest number in the row.
- Choose three or four allowed inputs. Include the smallest allowed input. For the card problem, add a row with a repeated number as well.
- Work out the correct output for each chosen input by hand and write it beside the input. Keep the table for testing.
The trap
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.
3.2 Write an unambiguous instruction
The rule
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.
How
- Number the steps and write each one as a short instruction.
- Give a name to each thing the steps use, such as the noted number, and use the same name every time.
- Replace each vague word with a test a reader can carry out. Step 3 of the largest-card procedure does not say a big card. It names one card, the card taken in step 2, and one test: its number is larger than the noted number.
- For each question a step asks, say what to do for yes. Say what to do for no as well, unless the reader then goes on to the next step in order, as after step 3 of the largest-card procedure. Step 2 covers both answers: go to step 5 when no card is left, and take the next card otherwise.
- Say where to go after a step whenever that is not the next step in order.
- Read the steps as a reader who knows nothing else would, and mark any place where two actions are possible.
The trap
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.
3.3 Trace a decision
The rule
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.
How
- Draw a table with a column for the step, a column for the condition with its values filled in and a column for what the procedure holds after the step.
- Write the input above the table.
- Carry out the steps in order. At a selection step, copy the condition with the real values in place of the names.
- Write true or false beside the condition. If it is true, do the action the step gives for true. If it is false, do the step's otherwise action when it has one. After either, follow any instruction about where to go next, and move to the next step in order when there is none.
- Stop at the step that says stop. The output is what that step announces.
- Trace again with inputs that take the other paths: one input that makes each selection step's action run, and one for which no condition is true. For the late-fee procedure,
0,3and8days late do that.
The trap
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.
3.4 Explain when a repetition ends
The rule
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.
How
- Find the steps that repeat, and mark the step that sends the reader back.
- Find the stopping condition, which is the test that lets the reader leave the loop.
- Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.
- Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.
- Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.
- Write one sentence that names the stopping condition and the step that moves toward it.
The trap
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.
3.5 Check a procedure against a counterexample
The rule
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.
How
- Take the table of inputs and correct outputs made with the problem statement, as in lesson 1.
- Add inputs at the extremes of what is allowed, such as the smallest allowed row. Add unusual rows too: a row with a repeated value, and a row whose numbers are all below zero.
- Trace the procedure on each input, and write what it announces beside the expected output.
- Mark each row pass or fail. A failed row is a counterexample.
- For a counterexample, identify the error and correct it. One way to find it is to look for the first step where the trace differs from a correct run. Correct that step and any later step that depends on it.
- Carry out every test again after the correction, including the tests that passed before.
The trap
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
4.1 Count comparisons in a linear search
The rule
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.
How
- Write the row and the target.
- Compare the target with the first card and count 1.
- If the card does not match and a card is left after it, move to the next card, compare and add 1 to the count. Repeat until a card matches or no card is left.
- Announce the result: the position of the first matching card, or not found after the last card.
- Check the count: it equals the position of the matching card, or the number of cards when nothing matched.
The trap
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.
4.2 Keep a sorted interval for binary search
The rule
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.
How
- Check that the row is sorted, smallest first. If it is not, binary search cannot be used.
- This lesson numbers positions from 1: write low = 1 and high = the number of cards, so the interval starts as the whole sorted row.
- If low is greater than high, the interval is empty: announce not found and stop.
- Work out middle = (low + high) / 2, rounded down: (1 + 2) / 2 is 1.5, which rounds down to 1. Read the card at that position. The search starts at the middle of the interval.
- Compare the target with that card and count 1 check: on a match, announce found at position middle and stop. For a smaller target write high = middle - 1, for a larger target write low = middle + 1, and go back to the third step.
The trap
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.
4.3 Insert one card into sorted order
The rule
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.
How
- Write the sorted row and the new card.
- Compare the new card with the last card, and count 1.
- If the card compared is not larger, place the new card directly to its right: the insertion is done.
- If no card is left to its left, place the new card first: the insertion is done. Otherwise compare with the next card to the left, count 1 and go back to the third step.
- When the insertion is done, check that the new row is sorted and holds every card once.
The trap
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.
4.4 Trace a sort on repeated keys
The rule
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.
How
- Write the unsorted row with each card told apart, and an empty sorted row.
- Mark the first unsorted card as the smallest so far.
- Compare the marked card with each later unsorted card in turn, counting each comparison. When a card's key is smaller than the marked card's key, move the mark to that card, and compare the cards after it with the newly marked card.
- Move the marked card to the end of the sorted row, and close up the unsorted row without changing its order.
- Repeat until no unsorted card is left, then add up the comparisons.
The trap
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.
4.5 Compare work as the collection doubles
The rule
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
How
- Choose the measure of size, such as the number of cards, and the step to count, such as a comparison.
- Write the worst-case count for one size.
- Double the size and write the worst-case count again.
- Compare the new count with the old one to see how the count grows.
- Compare the growth of the procedures, not their counts on one small input.
The trap
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
5.1 Agree on a message format
The rule
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.
How
- List the fields the protocol requires, in order.
- For each data card, check that every field is there.
- Check that the fields are in the agreed order: To, From, Piece, Data.
- Check the Piece field: a number from 1 up to the total, written as a number of the total.
- Check the Data field: at least 1 and no more than 4 characters.
- Mark the card as following the protocol only when every check passes.
The trap
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.
5.2 Split a message into numbered pieces
The rule
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.
How
- Count the characters of the message, spaces and punctuation included.
- Count the full groups of 4 characters, and add one more card if any characters are left over: that is N, the number of pieces.
- Cut the message into pieces of 4 characters from the left; the last piece takes what is left.
- Write one card for each piece, with To, From, its number of N and its data.
- Check that joining the data of cards 1 to N in order gives back the message.
The trap
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.
5.3 Reassemble pieces arriving out of order
The rule
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.
How
- Check each arriving card against the protocol. Read the total on the first usable data card and lay out that many empty slots.
- Put each usable card into the slot of its piece number if that slot is empty; put a card that fails the check aside.
- After each card, list the slots still empty.
- When no slot is empty, read the data of the slots from 1 to N.
- If a slot stays empty, the message is not complete; do not guess its data.
The trap
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.
5.4 Use acknowledgments in a paper protocol
The rule
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.
How
- Write each piece on the sender's list of pieces sent and not yet acknowledged, then send it and start its wait.
- On the receiver's side, check each card against the protocol, place it in its slot as in the last lesson (a slot already filled keeps its first card, as the next lesson shows), and send
ACK nfor each usable card. - For each acknowledgment that reaches the sender, cross its piece off the list.
- When a piece's wait runs out and the piece is still on the list, send it again and start its wait again.
- Stop when every piece has been sent and the sender's list is empty.
The trap
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.
5.5 Explain a duplicate request
The rule
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.
How
- When a card arrives, check it against the protocol; a card that breaks it is put aside without an acknowledgment.
- For a usable card, look at the slot of its number.
- If the slot is empty, place the card there and send
ACK n. - If the slot is filled, the card is a duplicate: leave the slot as it is and send
ACK nagain. - On the sender's side, cross the piece off at the first
ACK nand ignore any later one.
The trap
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.