Edge cases
Hidden tests love the corners of a problem. Before you submit, ask yourself:
- Empty input: an empty list, an empty string, zero.
- A single element, where there is no neighbour to compare.
- Duplicates: equal values, repeated words.
- Negatives and zero: sorting and multiplying behave differently.
- Ties: what should win when two answers are equally good?
The statement decides each answer, so read for the sentence that covers the corner. Then write that call yourself:
print(solution([])) # what should this be? print(solution([5, 5])) # and this?
See it run
def biggest(nums): return max(nums) print(biggest([4, 9])) print(biggest([7])) print(biggest([]))
Three calls: a normal list, a single element, and an empty list.
Practice edge cases
Exercise 1
Write solution(nums) returning the second largest distinct value in nums, or None when there are fewer than two distinct values. For example, solution([4, 9, 2]) is 4, and solution([5, 5, 5]) is None. Values can be negative, and the list can be empty.
Exercise 2
entries is a list of [name, score] pairs. Write solution(entries) returning the name with the highest score. If several names tie for the highest score, return the one that comes first alphabetically. An empty list gives "". Scores can be negative. For example, solution([["ann", 5], ["bo", 7]]) is "bo" and solution([["zed", 9], ["amy", 9]]) is "amy".
Exercise 3
Write solution(nums) that collapses each run of equal neighbours into one value and returns the new list. Equal values that are not next to each other all stay. For example, solution([1, 1, 2, 2, 2, 1]) is [1, 2, 1] and solution([4, 4]) is [4]. An empty list gives [].
Exercise 4
Write solution(nums) returning the largest product of two numbers taken from different positions in nums. There are at least 2 numbers, and they can be negative or zero. For example, solution([3, 5, 2]) is 15 and solution([-4, -3, 1]) is 12.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.