AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Which is better?

Week 9 · 1.6 Comparing algorithms · 3 lessons
1

Tick the requirements

Computing 9: Topic 1.6, pp. 119–127
GoalCompare two programs by checking each one against the requirements, and say which is more robust.
New words
requirement · one job that a program has to do, written down before the program is maderobust · reliable: less likely to give a silly answer or crash when the input is oddcompare · to put two things side by side and look for the differences that matter
Explanation

Two programmers can solve the same problem in very different ways, and both programs may work. To judge them fairly, start with the requirements, which are the jobs the program must do. Read the requirements one at a time and find the line of each program that does the job. A program that leaves out a requirement is not a correct solution. Some programs also do extra work. Code that checks the input before using it makes a program robust, because it stops a silly result or a crash. An extra feature only counts as better when it helps the user or protects the program.

Worked examples
Python
1celsius = float(input("Temperature in C? "))
2fahrenheit = celsius * 9 / 5 + 32
3print("In F:", fahrenheit)
Output
In F: 212.0
A converter. The requirements are: 1. ask for a temperature in degrees Celsius; 2. output the same temperature in degrees Fahrenheit (multiply by 9, divide by 5, add 32). Program A does this. The user types 100, and the program prints 212.0. Line 1 meets requirement 1, and lines 2 and 3 meet requirement 2.
Python
1celsius = float(input("Temperature in C? "))
2if celsius < -273:
3    print("Impossible: too cold")
4else:
5    fahrenheit = celsius * 9 / 5 + 32
6    print("In F:", fahrenheit)
Output
Impossible: too cold
Program B meets the same two requirements and adds a check. Nothing can be colder than about −273 °C, so a lower number is not a real temperature. The user types −300, and B prints a message. A would print In F: -508.0 for the same input, which looks like an answer but is impossible. This extra check makes B more robust.
CheckProgram AProgram BProgram C
1. Asks for the temperatureyesyesyes
2. Outputs the Fahrenheit valueyesyesno
Extra: rejects impossible inputnoyesno
Verdictcorrectcorrect and robustnot correct
A checklist for three programs. Program C is a third attempt: it prints the temperature it was given and never converts it. Reading the table, A and B meet both requirements, and B has a helpful extra. C fails requirement 2, so C is not a correct solution.
Class activity

House drawers (no computer). Write three requirements for a drawing on a card: 1. a square house; 2. a triangle roof on top; 3. a door in the middle of the bottom edge. Each person writes numbered instructions for it, such as ‘draw a square with sides 4 cm’, without showing them. Swap sheets and follow the other person’s instructions exactly. Tick each requirement on the card. Then add one robust extra to your instructions, for example ‘if the pencil line is too faint, go over it again’, and decide whether it really helps. (If you have a computer or tablet, you can type program A and program B from this lesson and try the inputs 20 and −300.)

Practice
1
The requirements are: 1. ask for the price in cents; 2. ask for the amount paid in cents; 3. output the change. Which requirements does this program meet? What does it print when the price is 250 and the amount paid is 200? Is that a good result?
Python
1price = int(input("Price in cents? "))
2paid = int(input("Paid in cents? "))
3print("Change:", paid - price)
2
Fix the problem in the previous program. Write the lines that make it robust, so that it outputs Not enough money when the amount paid is less than the price.
3
The requirement is: ask for a number of minutes and output the number of seconds. The user types 5. Trace this program. Does it meet the requirement?
Python
1minutes = int(input("Minutes? "))
2print(minutes + 60)
4
This program shares 12 sweets between friends. What does it print when the user types 4, and what happens when the user types 0? Write a version that is robust.
Python
1friends = int(input("How many friends? "))
2print("Each gets", 12 / friends)