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.
count_vowels(text) counts the vowels, capital or small. Example: count_vowels("Apple") → 2.
Logic error
is_registered(users, name) checks if a username exists. users maps IDs to names, like {"u1": "asha", "u2": "ravi"}. Example: is_registered(users, "asha") → True.
Logic error
age_next_year(age_text) gets an age typed into a form, as text, and returns next year's age as a number. Example: age_next_year("20") → 21.
Type error
An AI assistant wrote this FizzBuzz. fizzbuzz(n) returns "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both, and otherwise the number as text. Example: fizzbuzz(15) → "FizzBuzz".
Logic error
remove_word(words, target) removes every copy of target. Example: remove_word(["spam", "eggs", "spam"], "spam") → ["eggs"].
Logic error
capitalize_name(name) makes the first letter a capital and leaves the rest. Example: capitalize_name("asha") → "Asha".
Type error
countdown(n) returns the numbers from n down to 0. Example: countdown(3) → [3, 2, 1, 0].
Off-by-one
highest_temp(readings) returns the highest temperature of the day. Example: highest_temp([12, 18, 15]) → 18, highest_temp([-5, -2, -9]) → -2.
Logic error
An AI assistant wrote this to check if a word reads the same backwards, ignoring capitals. Example: is_palindrome("Racecar") → True, is_palindrome("hello") → False.
Type error
add_tag(tags, new_tag) adds a tag to the list and returns the list. Example: add_tag(["python"], "debugging") → ["python", "debugging"].
Other
An AI assistant wrote this for a classic interview question. has_pair_sum(numbers, target) returns True if two different numbers in the list add up to target. Example: has_pair_sum([1, 2, 3], 5) → True, has_pair_sum([5], 10) → False.
Logic error
append_to_list(val) puts val in a new list when no list is given. test_append() calls it with 1, then with 2. Example: test_append() → [[1], [2]].
Scope error
make_multipliers() returns 3 functions that multiply by 0, 1 and 2. test_multipliers() calls each with 10. Example: test_multipliers() → [0, 10, 20].
Scope error
total_values(data) adds up the values in a dictionary. Example: total_values({"a": 1, "b": 2}) → 3.
Other
sorted_names(names) returns the names in A to Z order. Example: sorted_names(["b", "a"]) → ["a", "b"].
Other
original_after_edit(grid) copies a grid, changes the copy, and returns the original's first cell, which should not change. Example: original_after_edit([[1, 2], [3, 4]]) → 1.
Other
safe_ratio(a, b) returns True when a / b is more than 1. If b is 0 it returns False instead of crashing. Example: safe_ratio(4, 2) → True, safe_ratio(1, 0) → False.
Logic error
largest_gap(nums) returns the biggest jump between neighbouring numbers in a list that goes up. Example: largest_gap([1, 5, 6]) → 4.
Logic error
all_positive(nums) returns True only when every number is above zero. Example: all_positive([1, -2]) → False.
Logic error
max_window_sum(nums, k) returns the biggest sum of k numbers in a row. Example: max_window_sum([1, 2, 3, 10], 2) → 13 (3 + 10).
Off-by-one
count_in_range(nums, low, high) counts the values from low to high, including both ends. Example: count_in_range([1, 2, 3], 1, 3) → 3.
Off-by-one
chunk(items, size) splits a list into groups of size, and keeps a shorter last group. Example: chunk([1, 2, 3, 4, 5], 2) → [[1, 2], [3, 4], [5]].
Off-by-one
last_char(text) returns the last character. Example: last_char("hello") → "o".
Off-by-one
total_score(scores, names) adds up the scores of the named players. A player with no score counts as 0. Example: total_score({"a": 5}, ["a", "b"]) → 5.
Null / Undefined
first_match(items, target) returns the first item equal to target, or -1 if there is none. Example: first_match([1, 2, 3], 5) → -1.
Null / Undefined
Showing 126–150 of 184 Python challenges