10. Safe Min and Max
Difficulty: Easy · Topic: Core Python & data structures
Write fare_range(fares): return a tuple (lowest, highest) of the fares, ignoring missing values (None).
- If there is no fare at all (an empty input, or only
None), return(None, None)instead of raising. - A fare of
0is a real fare, not a missing one. faresmay be any iterable, including a generator that can be read only once.
Example: fare_range([120, None, 80, 200]) gives (80, 200).
Your code runs in CPython on our servers against 6 tests, including hidden ones.
Sample test
fare_range([120, None, 80, 200])Expected: (80, 200)
Hints
Hint 1
min and max accept default=: what to return for an empty input instead of raising ValueError.Hint 2
Filter withis not None, not with plain truthiness: 0 is false.Hint 3
A generator is used up after one pass. Collect the values into a list once, then take both min and max.Learn the concepts
- Strings and built-in functions · 6 min read. split, join, strip, f-strings, zip, enumerate, any, all, min, max and sorted, and their surprises.
More Python problems
- Flatten a Nested List · Easy
- Squares of the Even Numbers · Easy
- Top K Frequent Words · Easy
- Invert a Dictionary · Easy
- Merge Dicts and Sum Shared Keys · Easy
- Group Records by Key · Easy
- Count Words in Reviews · Easy
- Zip Columns into Records · Easy
- Find the Missing IDs · Easy
- Sort by Several Keys · Medium
- Fix the Mutable Default Argument · Medium
- Override a Config Without Changing It · Medium
- Keep the Last N Events · Medium