#separator:tab
#html:true
#tags:algorithms-and-programs-quiz
#columns:Front Back
Someone holds up coloured cards, and you follow four steps: look at the card, decide which colour it is, write down its symbol, wait for the next card. What makes these instructions an algorithm?
- That they could be transferred to a computer, since only what a machine could execute counts as an algorithm.
- That the steps are described so precisely that you can carry them out without understanding what they are for.
- That they consist of a few manageable steps that you can remember without writing them down.
- That they are written in a programming language, since only a fixed syntax turns instructions into an algorithm.
- That the steps are described so precisely that you can carry them out without understanding what they are for.
Exactly the definition: an unambiguous sequence of steps that leaves no question open. Whoever follows it acts as the machine.
An algorithm is defined by the precision of its steps, not by the machine or the language.
What is the difference between an algorithm and a program?
- A program is the advanced stage of an algorithm, needed only once a problem becomes too large to handle by hand.
- An algorithm describes how to compute; a program defines how the data for it are stored and managed.
- A program is the same procedure, but written down so completely that a machine can carry it out.
- There is no essential one: both are unambiguous sequences of steps, and which word you use depends on the field.
- A program is the same procedure, but written down so completely that a machine can carry it out.
Exactly: a human fills in what was obviously meant, a machine does not, and that is why a program has to be complete.
A program is a list of instructions that a computer follows exactly and in order, so it must not rely on anyone filling in what was meant.
What do you see on the lamp when this program runs?
lamp.set_color(255, 0, 0)
time.sleep(1)
lamp.set_color(0, 0, 255)
- One second of red, then blue.
- Red and blue at once, so violet.
- Only blue, because the first line is skipped.
- Only red: the lamp keeps the first colour it receives until the program ends.
- One second of red, then blue.
Correct: line by line, from top to bottom. Red, wait one second, blue.
The computer carries out the lines in the order in which they stand, and each one changes the state that the next one finds.
"Someone is puzzled: ""The lamp never turns red, but the line is right there!"" What is going on?
lamp.set_color(255, 0, 0)
lamp.set_color(0, 0, 255)
- The first line contains a bug that the machine silently skips; otherwise red would have to show.
- The second line replaces the first before that one runs: with two commands of the same kind only the last one counts.
- Red does show, but only for a tiny fraction of a second; without a wait that is invisible to the eye.
- The lamp is sluggish: with two commands this close together only the last one gets through, and the first is lost in the hardware.
" - Red does show, but only for a tiny fraction of a second; without a wait that is invisible to the eye.
Exactly: the program is correct and only too fast. Your eyes are slow and the machine is not, so instructions take visible time only where a wait says so.
A computer waits only where the program tells it to wait.
What role does the part (255, 0, 0) play in this line?
lamp.set_color(255, 0, 0)
- It says which lamp is addressed: the three numbers identify which of several lamps is meant.
- It says with which values the instruction runs: full red, no green, no blue.
- It is a note for readers that explains the intended amount of red; only the part before the brackets is carried out.
- It says how long the instruction runs: for 255 milliseconds, after which the lamp is off again.
- It says with which values the instruction runs: full red, no green, no blue.
Correct: lamp says which thing, set_color says what should happen to it, and the brackets say with which values. Different values give a different result.
An instruction names a thing, what should happen to it and with which values; change the values, and the result changes while the instruction stays the same.
What is the benefit of the first line, bright = 255?
bright = 255
lamp.set_color(bright, 0, 0)
time.sleep(1)
lamp.set_color(bright, bright, 0)
- The program runs faster because the machine reads the value only once instead of anew at every call.
- Without it the lamp would stay dark, since this line is what sets the brightness to its maximum.
- The brightness can be changed in exactly one place instead of at every use.
- Names are mandatory: an instruction like
set_color accepts only named values, not bare numbers in the brackets.
- The brightness can be changed in exactly one place instead of at every use.
Exactly: a value with a name. To dim the lamp, you change one line, and both calls of set_color follow.
A value with a name gives you one place to change instead of many.
Roughly how long does this program run, and what does the lamp show?
colors = [(255, 0, 0), (0, 255, 0),
(0, 0, 255), (255, 255, 0)]
for r, g, b in colors:
lamp.set_color(r, g, b)
time.sleep(1)
- One second: the loop shows all four colours at once.
- About four seconds: four passes, each showing one colour for one second.
- Forever: a
for loop runs until you stop it. - Four seconds, but only the last colour, yellow, is ever visible.
- About four seconds: four passes, each showing one colour for one second.
Exactly: the same two lines run four times, each time with the next value from the list, and repetition costs time as well.
A loop is a way of writing: the same lines run once per value, one pass after another.
Roughly how long does this program run in total?
colors = [(255, 0, 0), (255, 255, 0), (0, 0, 255)]
for r, g, b in colors:
lamp.set_color(r, g, b)
if (r, g, b) == (255, 255, 0):
time.sleep(3)
else:
time.sleep(1)
- 3 seconds
- 9 seconds
- 5 seconds
- 4 seconds
- 5 seconds
Exactly: red 1, yellow 3, blue 1. The condition decides in every pass which branch with time.sleep runs.
A condition is checked anew in every pass, so the run depends on the value at hand.
Which building blocks is every program made of? Tick all that belong. (Several answers may be correct.)
- Instructions that run one after another.
- Values that get a name.
- Repetition.
- Conditions: something happens only if something holds.
- Instructions that run one after another.
Correct: the foundation, line by line from top to bottom. - Values that get a name.
Correct: one place to change instead of many. - Repetition.
Correct: the same lines run several times, each time with the next value. - Conditions: something happens only if something holds.
Correct: this is how a program reacts instead of always running through the same steps.
All four together are the whole building material: sequence, named values, repetition and condition.
"Your program stops with this message. What do you make of it?
Traceback (most recent call last):
File ""blink.py"", line 7, in <module>
lamp.set_colour(255, 0, 0)
AttributeError: 'Lamp' object has no attribute 'set_colour'. Did you mean: 'set_color'?
- The computer crashed; after a restart the program usually works again, since messages like this come and go.
- The message states file, line and cause: line 7 says
set_colour, which does not exist; set_color was meant. - The program overwhelms the machine; the message shows that it could not keep up when running line 7.
- The message is a warning you can read past: the program continues after line 7, and the lamp lights up anyway.
" - The message states file, line and cause: line 7 says
set_colour, which does not exist; set_color was meant.
Exactly: an error message is information, not a verdict. Read it out loud; it usually says literally what is wrong.
The computer carried out what was written and reports exactly where it got stuck; the last line of the message names the cause.
When does this program end?
while True:
lamp.set_color(255, 0, 0)
time.sleep(1)
lamp.set_color(0, 0, 255)
time.sleep(1)
- Once both colours have been shown, because then the body of the loop has been carried out completely.
- By itself after a while, when the machine notices that nothing new happens.
- Never: the condition is always true, and the body of the loop has no way out.
- After a fixed number of repetitions, because every loop has a built-in upper limit so that the machine does not get stuck.
- Never: the condition is always true, and the body of the loop has no way out.
Exactly: a procedure ends only if its end is reachable. This loop blinks until someone stops it from outside, which is even intended for a lamp that should blink continuously; a program that is meant to finish needs a termination condition.
A loop ends only when its end is reachable; while True without a way out runs for ever.
In the guessing game from 1 to 100, two procedures are certain to succeed: asking for every number one by one (up to 99 questions) or halving the range (at most 7 questions). What follows?
- Only halving deserves the name algorithm; asking one by one is mere trial without a procedure.
- A solution can be correct and still useless; procedures are compared by their cost.
- On modern computers the difference no longer matters, since both procedures finish in a fraction of a second anyway.
- The slow procedure is safer because it checks every number explicitly, and is therefore preferable.
- A solution can be correct and still useless; procedures are compared by their cost.
Exactly: being correct is the minimum, and after that the cost counts, here the number of questions.
What matters is how the cost grows with the size of the problem.
Six people sort themselves by height with these rules: look at your right neighbour, swap places if you are taller, start again from the left at the end of the row. How does this procedure know that it is finished?
- After exactly one pass through the row, because by then every person has checked their neighbour once.
- When the people can see that the row looks sorted.
- By a complete pass in which nobody swapped.
- Not by itself: as with any repetition that starts over, someone from outside has to stop it once the row is right.
- By a complete pass in which nobody swapped.
Exactly: that is the built-in termination condition. No swap in a whole pass means that nothing is left to sort, and without such a condition the procedure would run for ever.
Bubble sort ends by its own rule: a pass without a swap.
What does the lamp show when this program runs?
lamp.set_color(255, 0, 0)
lamp.set_color(0, 0, 255)
time.sleep(1)
- Red for one second, then blue: the
time.sleep(1) obviously belongs to the red phase. - Blue for one second, then red: the pause holds the colour set last, and the first line is caught up on afterwards.
- Red only for a split second, then blue for one second.
- The program is faulty and does not start, because
time.sleep(1) is not allowed as the last line.
- Red only for a split second, then blue for one second.
Exactly: blue replaces red at once, and only then does the machine wait, with the lamp blue. The same three lines with the pause in the middle would be a different program, because the order is part of the program.
No line is missing, and still the order makes it a different program.