Edge cases with lists and strings

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?

Edge cases with lists and strings

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which list is an edge case you should test for a function that finds the biggest number?
A [] (an empty list)
B [4, 9, 2] (a few different numbers)
2. A program finds the largest score in a list. Which test checks the 'maximum at the start' edge case?
A [40, 55, 90, 12]
B [90, 40, 55, 12]
3. A program finds the largest score in a list. Which test checks the 'maximum at the end' edge case?
A [12, 40, 55, 90]
B [12, 90, 55, 40]
4. Which test data is a single-item edge case?
A [7, 7, 7]
B [7]
5. True or false: an edge case must be invalid input that the program should reject.
A False: edge cases are unusual but allowed inputs
B True: edge cases are always invalid inputs

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight 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].

Definitions

edge case

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def biggest(nums): if ___(nums) == 0: return None
2.
scores = [42] print(scores[_]) print(scores[-1])
3.
nums = [5, 9, 9, 2] print(nums._____(9))
4.
nums = [2, 4, 9] print(nums[__] == max(nums))
5.
word = "" if ___ word: print("Empty string")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def average(nums):
B if len(nums) == 0:
C return nums[0]
D return sum(nums) / len(nums)
Question 2
A def last_item(items):
B return items[len(items)]
C print(last_item([7]))
D print(last_item([3, 3, 3]))
Question 3
A nums = [9, 4, 2]
B pos = 1
C for i in range(len(nums)):
D if nums[i] > nums[pos]:
E pos = i
Question 4
A def count_a(text)
B n = 0
C for ch in text:
D if ch == "a":
E n += 1
F return n
Question 5
A word = "level"
B rev = word[::-1]
C if word = rev:
D print("Palindrome")

Answer Key

Edge cases with lists and strings
Section A: Quick Questions
Q1
A — [] (an empty list)
Q2
B — [90, 40, 55, 12]
Q3
A — [12, 40, 55, 90]
Q4
B — [7]
Q5
A — False: edge cases are unusual but allowed inputs
Section B: Match the Pairs
Q1
max([]) → Edge case: an empty list. Python raises a ValueError because there is no largest item to return.
Q2
scores = [7, 7, 7, 7] → Edge case: duplicate values. Every item is the same, so tests should check that code handles ties.
Q3
A one-item list is an edge case because the first item is also the last item and the maximum. → nums = [42] print(nums[0] == nums[-1])
Q4
The maximum at the end of a list is an edge case that tests whether a loop checks every item. → test = [3, 8, 2, 9] print(max(test) == test[-1])
Q5
biggest = nums[0] for i in range(1, len(nums) - 1): if nums[i] > biggest: biggest = nums[i] → The 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].
Q6
def first_item(items): return items[0] print(first_item([])) → The code never handles an empty list, so items[0] causes an IndexError. It needs a check such as if len(items) == 0 first.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
def biggest(nums): if len(nums) == 0: return None
Q2
scores = [42] print(scores[0]) print(scores[-1])
Q3
nums = [5, 9, 9, 2] print(nums.count(9))
Q4
nums = [2, 4, 9] print(nums[-1] == max(nums))
Q5
word = "" if not word: print("Empty string")
Section E: Spot the Error
Q1
C — For an empty list there is no item at index 0, so nums[0] raises an IndexError. The empty-list case should return a safe value such as 0.
Q2
B — Indexes stop at len(items) - 1, so items[len(items)] is always out of range, even for a one-item list. Use items[-1] or items[len(items) - 1].
Q3
B — Starting pos at 1 skips the first item, so a maximum at the start of the list (like 9) is never chosen. pos should start at 0.
Q4
A — The def line is missing its colon, so the function will not run at all and edge cases like an empty string cannot be tested. It should be def count_a(text):
Q5
C — A single = is assignment, not comparison, so this is a syntax error. Use == to compare the word with its reverse, which also handles one-letter strings.