Lesson 7.4 · Answer it
All lights on cases
4 marksYou mark it yourself against the marking points
A building has n lights in a row, each on or off. This function checks whether they are all on. It returns as soon as it finds one that is off.
def all_on(lights):
for light in lights:
if not light:
return False
return True
public static boolean allOn(boolean[] lights) {
for (boolean light : lights) {
if (!light) {
return false;
}
}
return true;
}
(a) Describe the input of n lights that gives the best case. [1]
(b) Describe an input of n lights that gives the worst case. [1]
(c) A student writes: "The best case is when there are no lights, so the function is O(1)." Explain two things that are wrong with this claim. [2]
[4]