Biggest gap between numbers comes out as -1
largest_gap(nums) returns the biggest jump between neighbouring numbers in a list that goes up. Example: largest_gap([1, 5, 6]) → 4.
Explore 184 Python challenges. Filter by bug pattern or difficulty.
describe_temp(readings, station) returns "recorded" when the station sent any reading, including 0 degrees, and "missing" when it sent none. Example: describe_temp({"north": 0}, "north") → "recorded".
Null / Undefined
retries_for(config) returns config["retries"] if it is set, or 3 if it is missing. 0 means "don't retry". Example: retries_for({"retries": 0}) → 0, retries_for({}) → 3.
Null / Undefined
middle_item(items) returns the item in the middle of the list. Example: middle_item([1, 2, 3]) → 2.
Type error
count_above(values, threshold) counts the values bigger than threshold. The values arrive from a form as text. Example: count_above(["1", "5", "10"], 3) → 2.
Type error
total_for_ids(counts, ids) adds up the counts for the given ids. counts comes from JSON, so its keys are text, like {"1": 10}. The ids are numbers. Example: total_for_ids({"1": 10, "2": 20}, [1, 2]) → 30.
Type error
highest(values) returns the biggest number from a list of numbers written as text. Example: highest(["9", "10", "2"]) → "10".
Type error
shrink_to_one(n) counts the steps to reach 1. Each step: halve n if it is even, or make it 3n + 1 if it is odd. Example: shrink_to_one(6) → 8.
Infinite loop
attempts_until_success(n) keeps trying until try number n succeeds, and returns how many tries it took. Example: attempts_until_success(3) → 3.
Infinite loop
squash_spaces(text) turns any run of spaces into one space. Example: squash_spaces("a b") → "a b".
Infinite loop
count_pairs(nums, target) counts the pairs of numbers that add up to target. Example: count_pairs([1, 2, 3], 5) → 1 (2 + 3).
Infinite loop
Each Basket should have its own list of items. two_baskets(first, second) adds one item to each of two baskets and returns how many items the second has. Example: two_baskets("x", "y") → 1.
Scope error
add_all(nums) returns the sum of the numbers. Example: add_all([1, 2, 3]) → 6.
Scope error
make_multipliers(factors) makes one multiplier function per factor. apply_first uses the first one. Example: apply_first([2, 3, 4], 10) → 20.
Scope error
last_even(nums) returns the last even number, or -1 if there are none. Example: last_even([1, 2, 3, 4]) → 4, last_even([1, 3]) → -1.
Scope error
checkout(total, pct) takes pct percent off the total. This project has two files: cart.py and pricing.py. Example: checkout(200, 50) → 100.
Logic error
merge_settings(defaults, overrides) returns a new dict with the overrides on top, and leaves defaults unchanged so it can be reused. check_merge only reports [merged mode, merged size, defaults mode, defaults size]. Fix merge_settings. Example: check_merge({"mode": "light"}, {"size": "big"}) → ["light", 2, "light", 1].
Other
shout(text) returns the text in capital letters. Example: shout("hello") → "HELLO".
Other
count_words(text) counts the words, however many spaces are between them. Example: count_words("hello world") → 2.
Logic error
remove_evens(nums) returns the list without even numbers. Example: remove_evens([2, 4, 6, 1]) → [1].
Other
with_theme(settings, theme) changes the theme in a copy and keeps the original as it was. It returns "new theme|original theme" so you can see both. Example: with_theme({"display": {"theme": "light"}}, "dark") → "dark|light".
Other
make_baskets(first, second) puts each item in its own new basket. Example: make_baskets("apple", "bread") → [["apple"], ["bread"]].
Scope error
average(nums) returns the average of the numbers, including any decimal part. Example: average([1, 2]) → 1.5.
Type error
is_leap_year(year) returns True for leap years. The rule: divisible by 4, but not by 100, unless also divisible by 400. Example: 2024 → True, 1900 → False, 2000 → True.
Logic error
sum_digits(n) adds up the digits of a number. Example: sum_digits(123) → 6 (1 + 2 + 3).
Infinite loop
grade(score) returns "A" for 90 and above, "B" for 80–89, "C" for 70–79, "D" for 60–69 and "F" below 60. Example: grade(95) → "A".
Logic error
Showing 151–175 of 184 Python challenges