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.
checkout(cart) saves the items as a receipt, empties the cart, and returns the receipt. Example: checkout(["pen", "ink"]) → ["pen", "ink"].
Other
mentions(text, word) returns True if word appears anywhere in text. Example: mentions("hello world", "hello") → True.
Logic error
parse_flag(text) reads a setting written as "true" or "false", in any case, and returns True or False. Example: parse_flag("False") → False.
Type error
first_and_last(full_name) returns the first and last name. Example: first_and_last("Mary Ann Smith") → ["Mary", "Smith"].
Other
average_of_two(a, b) returns the average of two marks. Example: average_of_two(4, 6) → 5.
Logic error
square(n) returns n squared. Example: square(3) → 9.
Other
contains(items, target) returns True if target is anywhere in the list. Example: contains([4, 8, 15], 8) → True.
Logic error
order_total(prices) adds up every price. Example: order_total([5, 10, 15]) → 30.
Logic error
percent_score(correct, total) returns a whole-number percentage. Example: percent_score(3, 4) → 75.
Type error
is_weekend(day) returns True for "Saturday" and "Sunday", and False for other days. Example: is_weekend("Monday") → False.
Logic error
make_greeting(name) returns a greeting for the welcome page. Example: make_greeting("Asha") → "Hello, Asha!".
Other
top_three(scores) returns the 3 highest scores, best first. Example: top_three([50, 90, 70, 80]) → [90, 80, 70].
Logic error
check_answer(answer) marks the answer to "What is the capital of France?", ignoring capitals and extra spaces. Example: check_answer("Paris ") → True.
Logic error
unique_tracks(track_ids) removes repeated tracks and keeps the original order. Example: unique_tracks([30, 10, 30, 20]) → [30, 10, 20].
Other
book_seat(rows, cols, row, col) makes an empty seating grid and books one seat. Example: book_seat(2, 2, 0, 0) → [[1, 0], [0, 0]].
Other
total_price(prices) adds up every price. Example: total_price([10, 20, 30]) → 60.
Logic error
is_prime(n) returns True if n is a prime number. Example: is_prime(7) → True, is_prime(8) → False, is_prime(1) → False.
Logic error
An AI assistant wrote this for a classic interview question. is_anagram(a, b) returns True when both words use the same letters, the same number of times. Example: is_anagram("listen", "silent") → True, is_anagram("aab", "abb") → False.
Logic error
is_admin(role) returns True for "admin" in any mix of capitals. Example: is_admin("Admin") → True, is_admin("user") → False.
Logic error
csv_total(line) adds up numbers separated by commas. Example: csv_total("4,5,6") → 15.
Type error
most_common_letter(word) returns the letter that appears most often. Example: most_common_letter("banana") → "a".
Null / Undefined
parse_price(text) turns a typed price into a number. Example: parse_price("3.50") → 3.5, parse_price("12") → 12.
Type error
An AI assistant wrote this to find the second-best score in a quiz. second_highest(scores) returns the second-highest different score. Example: second_highest([70, 90, 80]) → 80, second_highest([90, 90, 80]) → 80.
Logic error
cart_total(prices) adds up every price. Example: cart_total([10, 20, 30]) → 60.
Off-by-one
round_mark(mark) rounds an exam mark the school way: .5 always goes up. Example: round_mark(2.5) → 3, round_mark(2.4) → 2.
Other
Showing 101–125 of 184 Python challenges