Algorithms and computational thinking
Picture the crosswalk button outside your high school: you press it, wait, and the light changes in a set order. Before you write a single line of code, you need to be completely clear about the steps you have to take. That list of exact steps is an algorithm, and thinking in this way — breaking things down, looking for patterns, keeping only what matters — is computational thinking. By the end of this page you will be able to describe that crosswalk as an algorithm.
What an algorithm is
Write the algorithm, as a numbered list of steps, to make a cheese sandwich, so that a person who has never made one can follow it without doubt. Review your list and…
Write the algorithm, as a numbered list of steps, to make a cheese sandwich, so that a person who has never made one can follow it without doubt. Review your list and check that no steps you took for granted are missing.
Write the algorithm for preparing a glass of chocolate milk in six to eight steps, so precise that a robot could follow them: quantities, order of steps, and how to…
Write the algorithm for preparing a glass of chocolate milk in six to eight steps, so precise that a robot could follow them: quantities, order of steps, and how to check that the cocoa has dissolved before finishing.
In the "frying an egg" recipe, identify the input (ingredients and utensils), the process (steps involving heat and the frying pan), and the output (the result). Explain…
In the "frying an egg" recipe, identify the input (ingredients and utensils), the process (steps involving heat and the frying pan), and the output (the result). Explain why thinking in this way helps to write clear algorithms.
A recipe says "stir the sauce until it thickens". Explain why an algorithm must always terminate and what could happen if the sauce never thickens. Rewrite that step so…
A recipe says "stir the sauce until it thickens". Explain why an algorithm must always terminate and what could happen if the sauce never thickens. Rewrite that step so that the cook does not end up stirring forever.
A recipe says: "add salt and simmer for a while on medium heat". Explain why a computer could not follow that instruction and rewrite it as a precise algorithm step,…
A recipe says: "add salt and simmer for a while on medium heat". Explain why a computer could not follow that instruction and rewrite it as a precise algorithm step, stating quantities, timings, and how to check that the step is complete.
Decisions and repetitions in the flowchart
On the AP exam you will mostly see algorithms written in pseudocode instead of flowcharts. The three building blocks look like this:
count ← 0
REPEAT UNTIL (count = 3) {
IF (count MOD 2 = 0) {
DISPLAY("even")
} ELSE {
DISPLAY("odd")
}
count ← count + 1
}
The ← stores a value, REPEAT UNTIL is the iteration (it stops when the condition becomes true) and IF/ELSE is the selection. When you know how many times to repeat, use REPEAT n TIMES instead, for example REPEAT 3 TIMES { DISPLAY("Hi") } displays Hi three times.
These steps of the "going to school" algorithm are out of order: (a) enter the classroom, (b) get up, (c) have breakfast, (d) leave the house, (e) get dressed, (f) walk…
These steps of the "going to school" algorithm are out of order: (a) enter the classroom, (b) get up, (c) have breakfast, (d) leave the house, (e) get dressed, (f) walk to school. Put them in the correct order and add a decision to the algorithm: what to do if it is raining when leaving the house.
The figure shows, using interlocking blocks, the algorithm for an automatic reading lamp that should turn on only when light is low, but one block is missing. Indicate…
The figure shows, using interlocking blocks, the algorithm for an automatic reading lamp that should turn on only when light is low, but one block is missing.
Indicate which block you would place in the gap and explain in your own words how the complete algorithm works, step by step.
Read this algorithm: (1) pick up the deck of cards, (2) while cards remain in your hand, deal one to each player starting on your left, (3) when no cards remain,…
Read this algorithm: (1) pick up the deck of cards, (2) while cards remain in your hand, deal one to each player starting on your left, (3) when no cards remain, announce "dealt". Explain in one sentence what the algorithm does and point out which part is the loop and when it terminates.
Trace this algorithm with $n = 7$: (1) start with the value of $n$, (2) repeat three times: add 2, (3) subtract 5, (4) write the result. Write down the value after each…
Trace this algorithm with $n = 7$: (1) start with the value of $n$, (2) repeat three times: add 2, (3) subtract 5, (4) write the result. Write down the value after each step, give the final result, and explain what a trace is and why performing it by hand is useful.
Write the algorithm to count how many people enter through the classroom door using only a "counter" variable starting at 0: what you do each time someone enters, how…
Write the algorithm to count how many people enter through the classroom door using only a "counter" variable starting at 0: what you do each time someone enters, how you know when to stop counting, and what value the counter will have at the end.
Decomposing, recognizing patterns and abstracting
Decompose the problem "organizing the end-of-year party" into four smaller sub-problems and write two concrete steps to solve each one. Explain the advantage of breaking…
Decompose the problem "organizing the end-of-year party" into four smaller sub-problems and write two concrete steps to solve each one. Explain the advantage of breaking down a large problem in this way.
To sort a deck of cards there are several different algorithms, and some are faster than others. Explain with an everyday example how the same problem can be solved by…
To sort a deck of cards there are several different algorithms, and some are faster than others. Explain with an everyday example how the same problem can be solved by multiple algorithms and what it means for one to be "better" than another.
Recognizing patterns means finding the repeating rule. Find the pattern in these three sequences and express it as a clear rule: (a) 5, 10, 15, 20, ..., (b) 100, 90, 80,…
Recognizing patterns means finding the repeating rule. Find the pattern in these three sequences and express it as a clear rule: (a) 5, 10, 15, 20, ..., (b) 100, 90, 80, 70, ..., (c) Monday: 1 push-up, Tuesday: 2, Wednesday: 4, Thursday: 8, ... State what value would come next in each one.
A subway map does not draw the real city: it removes distances, curves, and streets, keeping only stations and connections. Explain what abstraction is using this…
A subway map does not draw the real city: it removes distances, curves, and streets, keeping only stations and connections. Explain what abstraction is using this example and then apply it to the floor plan of your school: which three features would you include and which three would you remove?
You can do it on paper and send a photo with your answer.
You are looking for a word in a 500-page dictionary. Compare these two algorithms: opening at the first page and advancing one page at a time, or opening in the middle…
You are looking for a word in a 500-page dictionary. Compare these two algorithms: opening at the first page and advancing one page at a time, or opening in the middle and discarding the unneeded half. Explain which is more efficient, why it only works if the pages are sorted, and estimate how many "page openings" each method requires.
Two people prepare breakfast together for four guests: coffee needs to be made, bread toasted, juice squeezed, and the table set. Divide the tasks between the two while…
Two people prepare breakfast together for four guests: coffee needs to be made, bread toasted, juice squeezed, and the table set. Divide the tasks between the two while respecting dependencies (you cannot serve coffee before making it) and explain which tasks can be done simultaneously and which cannot, giving their order.
Two algorithms are proposed for classroom recycling: (a) each day, the person responsible sorts all the day's trash into three bins, and (b) each week, in turns, a pair…
Two algorithms are proposed for classroom recycling: (a) each day, the person responsible sorts all the day's trash into three bins, and (b) each week, in turns, a pair sorts everything accumulated. Evaluate both against criteria of total time, sorting errors, and distribution of effort, and choose one with justification.
Edge cases and errors in algorithms
The algorithm for a cafeteria hands out a sandwich to each attendee, but fails when there are more attendees than sandwiches. Explain what an "edge case" is in an…
The algorithm for a cafeteria hands out a sandwich to each attendee, but fails when there are more attendees than sandwiches. Explain what an "edge case" is in an algorithm, propose how distribution should behave in that situation, and rewrite the problematic step so that the algorithm does not fail.
An ATM only contains $\$20$ bills and its algorithm dispenses the requested amount in bills. Explain what happens when someone requests $\$75$, why this is an edge case,…
An ATM only contains $\$20$ bills and its algorithm dispenses the requested amount in bills. Explain what happens when someone requests $\$75$, why this is an edge case, and rewrite the ATM rules so that it always provides a sensible response.
Design the complete algorithm, with numbered steps that include decisions and loops, for a crosswalk with a button: what happens from when someone presses the button…
Design the complete algorithm, with numbered steps that include decisions and loops, for a crosswalk with a button: what happens from when someone presses the button until the lights return to their initial state, including the timings for each light.
Think of a program that plays tic-tac-toe. Describe the strategy it would follow on each turn: what board data it must inspect, in what order, and what rules you would…
Think of a program that plays tic-tac-toe. Describe the strategy it would follow on each turn: what board data it must inspect, in what order, and what rules you would use to decide where to place the mark. There is no need to write code: state the rules in natural language.
The figure shows, using interlocking blocks, the skeleton of a secret number guessing game, but inside the loop a piece is missing so that the game can give clues to the…
The figure shows, using interlocking blocks, the skeleton of a secret number guessing game, but inside the loop a piece is missing so that the game can give clues to the player.
Describe which block (or blocks) you would place in the gap so that the game tells the player whether their number is greater or smaller than the secret number, and explain the complete operation of the algorithm: what is repeated, when it terminates, and what happens if the player gets it right on the first try.
This "doing the laundry" algorithm contains a bug: (1) put all clothes into the washing machine, white and colored together, (2) add detergent, (3) choose the long…
This "doing the laundry" algorithm contains a bug: (1) put all clothes into the washing machine, white and colored together, (2) add detergent, (3) choose the long hot-water program, (4) hang out to dry. Find two problematic steps, explain what could go wrong in each, and rewrite the corrected algorithm including a prior sorting step.