Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Edge cases with lists and strings worksheet is part of the ACARA Digital Technologies Years 9–10 — Testing and Validating Programs journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1max([]) | Anums = [42] print(nums[0] == nums[-1]) | |
| 2scores = [7, 7, 7, 7] | Btest = [3, 8, 2, 9] print(max(test) == test[-1]) | |
| 3A one-item list is an edge case because the first item is also the last item and the maximum. | CThe code never handles an empty list, so items[0] causes an IndexError. It needs a check such as if len(items) == 0 first. | |
| 4The maximum at the end of a list is an edge case that tests whether a loop checks every item. | DEdge case: an empty list. Python raises a ValueError because there is no largest item to return. | |
| 5biggest = nums[0] for i in range(1, len(nums) - 1): if nums[i] > biggest: biggest = nums[i] | EEdge case: duplicate values. Every item is the same, so tests should check that code handles ties. | |
| 6def first_item(items): return items[0] print(first_item([])) | FThe loop stops one item early, so it never checks the last item. It fails when the maximum is at the end, for example [1, 5, 9]. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
edge case — An unusual but allowed input, such as an empty list, a one-item list, repeated values, or the maximum at the start or end, that you test because it can break code.def biggest(nums):
if len(nums) == 0:
return Nonescores = [42]
print(scores[0])
print(scores[-1])nums = [5, 9, 9, 2]
print(nums.count(9))nums = [2, 4, 9]
print(nums[-1] == max(nums))word = ""
if not word:
print("Empty string")