Lesson 7.1 · Answer it
Count the comparisons
5 marksYou mark it yourself against the marking points
This function counts how many race times in a list are over a limit. Each time is a whole number of seconds.
def count_over(times, limit):
over = 0
for t in times:
if t > limit:
over = over + 1
return over
public static int countOver(
int[] times, int limit) {
int over = 0;
for (int t : times) {
if (t > limit) {
over = over + 1;
}
}
return over;
}
Count the comparison t > limit as one step.
(a) State how many times the comparison runs for a list of 200 times. [1]
(b) State, in terms of n, how many times the comparison runs for a list of n race times. [1]
(c) On Mei's laptop, a list of 2000 times takes 6 milliseconds. Assume the time grows in step with the number of comparisons. Estimate the time for a list of 20000 times on the same laptop. [1]
(d) The function is run on a list of 2000 times that are all over the limit, and then on a list of 2000 times that are all under it. State how many comparisons each run makes, and explain why. [2]
[5]