GoalCompare two programs in the right order (requirements first, then efficiency) and write a verdict with reasons.
New words
verdict · your final decision about which program is better, with reasonspass · one run of the body of a loopjustify · to give reasons that support a decision
Explanation
A fair comparison has an order. First, list the requirements and check that each program meets them. Remove any program that fails. Second, compare the efficiency of the programs that are left. Count the variables, because they use memory. You can also count the passes through a loop, because a program with fewer passes finishes sooner. Third, write your verdict and give reasons, such as ‘B is better because it has the same output but one variable fewer’. Sometimes the answer is mixed, for example when one program is more robust and the other is more efficient. Then say which matters more for the job. Experts have a more advanced method of measuring efficiency, called Big O notation, which you may meet later.
1. Check the requirementsonly programs that work→2. Compare the efficiencygive reasons→3. Write the verdict with reasons
Worked examples
Python
1for n inrange(5, 31, 5):
2print(n)
Output
5
10
15
20
25
30
Two programs that print the multiples of 5 up to 30. Program A lets the counter jump in steps of 5. It makes 6 passes and uses one variable, n. The printed output is 5, 10, 15, 20, 25, 30, one number per line.
Program B prints exactly the same numbers. It looks at every number from 1 to 30, adds 1 to step each time, and prints when step reaches 5. It makes 30 passes and uses two variables, n and step.
Check
Program A
Program B
Meets the requirement
yes
yes
Variables
1
2
Passes through the loop
6
30
Verdict
better
worse
The verdict. Both programs meet the requirement, so the efficiency can be compared. A is better on both counts. It uses fewer variables and makes fewer passes, so it uses less memory and finishes sooner.
Class activity
Water the plants (no computer). Five pots stand in a row, and the tap is at one end. Method A: fill a cup at the tap, water one pot, and walk back to the tap for the next cup. Method B: fill a jug at the tap once and water all five pots from it. Write each method as numbered steps. Act both out, with a pencil for a jug, and count the trips to the tap. Check that both methods meet the requirement that each pot gets water once. Then write a verdict of two sentences with a reason, and say which loop would have more passes if each method were a program.
Practice
1
The requirements are: ask for the number of apples and the number of baskets, and output how many apples go in each basket. Compare the programs. Which meets the requirements? Which is more robust? Which uses fewer variables? Give a verdict.
Python
1# Program L2apples = int(input("Apples? "))
3baskets = int(input("Baskets? "))
4each = apples / baskets
5print("Each basket:", each)
67# Program M8apples = int(input("Apples? "))
9baskets = int(input("Baskets? "))
10if baskets == 0:
11print("Need at least 1 basket")
12else:
13print("Each basket:", apples / baskets)
Both programs meet the requirements. For 12 apples and 4 baskets both print Each basket: 3.0. Program M is more robust, because for 0 baskets L stops with ZeroDivisionError: division by zero, but M prints a message. M also uses two variables, while L uses three. M is better on every check.
2
Both programs print 3, 6, 9, 12 and 15, one per line. How many passes does each loop make, and how many variables does each program use? Which is more efficient?
Python
1# Program J2for n inrange(3, 16, 3):
3print(n)
45# Program K6next_multiple = 37for n inrange(1, 16):
8if n == next_multiple:
9print(n)
10 next_multiple = next_multiple + 3
Program J makes 5 passes and uses one variable (n). Program K makes 15 passes and uses two variables (n and next_multiple). J is more efficient.
3
Put these steps of a fair comparison in the correct order: write the verdict; compare the efficiency of the programs that work; list the requirements; check each program against the requirements.
1. List the requirements. 2. Check each program against the requirements. 3. Compare the efficiency of the programs that work. 4. Write the verdict, with reasons.
4
Three programs have all been tested. Write a verdict. Which program do you choose, and why? Which program can you not choose?
Program
Requirements met
Robust?
Variables
P
3 of 3
no
5
Q
3 of 3
yes
4
R
2 of 3
no
1
Choose Q: it meets all the requirements, it is robust, and it has fewer variables than P. R cannot be chosen, even though it has only one variable, because it meets just 2 of the 3 requirements.