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.
sum_until_stop(readings) adds readings until it meets -1, which means "stop". Anything after -1 is ignored. Example: sum_until_stop([3, 4, -1, 10]) → 7.
Logic error
domain_of(url) removes "https://" from the front of a web address. Example: domain_of("https://shop.com") → "shop.com".
Other
build_pin(digits) joins a list of digits into a PIN. Example: build_pin([1, 2, 3, 4]) → "1234".
Type error
shipping_cost(total) returns 0 for orders of 50 or more, and 5 otherwise. Example: shipping_cost(20) → 5.
Null / Undefined
find_duplicates(nums) lists each repeated value once, in the order it first appears. Example: find_duplicates([1, 2, 2, 3]) → [2].
Logic error
parse_tags(text) turns comma-separated text into tags. Example: parse_tags("python, debugging") → ["python", "debugging"].
Other
serve_next(queue) removes and returns the person who has waited longest, at the front. Example: serve_next(["asha", "ravi", "zoe"]) → "asha".
Logic error
make_label(text, number) builds an order label. Example: make_label("Order", 5) → "Order #5".
Scope error
numbered(items) numbers the list starting from 1. Example: numbered(["eggs", "milk"]) → ["1. eggs", "2. milk"].
Off-by-one
add_items(cart, new_items) adds every item in new_items to the cart. Example: add_items(["pen"], ["ink", "pad"]) → ["pen", "ink", "pad"].
Other
An AI assistant wrote this for a recursion exercise. fib(n) returns the nth number in 0, 1, 1, 2, 3, 5, 8 … Example: fib(0) → 0, fib(1) → 1, fib(6) → 8.
Off-by-one
reverse_words(sentence) reverses the order of the words but keeps each word readable. Example: reverse_words("hello world") → "world hello".
Logic error
sort_contacts(names) sorts names A to Z, ignoring capitals. Example: sort_contacts(["bob", "Carl", "alice"]) → ["alice", "bob", "Carl"].
Logic error
passed_students(scores) returns the names of students with 40 or more, from a dict like {"asha": 72, "ravi": 35}. Example: passed_students({"asha": 72, "ravi": 35}) → ["asha"].
Other
top_scorer(scores) takes a dict of names and scores and returns the name with the highest score. Example: top_scorer({"asha": 90, "ravi": 95, "zoe": 70}) → "ravi".
Logic error
pages_needed(items, per_page) returns how many pages are needed, counting a partly full last page. Example: pages_needed(10, 4) → 3.
Type error
safe_ratio(done, total) returns done / total, or 0 when total is 0. Example: safe_ratio(1, 4) → 0.25, safe_ratio(0, 0) → 0.
Other
can_edit(role) returns True only for roles in the allowed list, which has just "admin". Example: can_edit("admin") → True, can_edit("adm") → False.
Type error
age_on(birth_iso, today_iso) returns how old someone is on a given date. Example: age_on("2000-12-25", "2024-06-01") → 23.
Logic error
preview(text, length) returns the first length characters. Example: preview("Hello world", 5) → "Hello".
Off-by-one
count_rises(prices) counts the days a price went up from the day before. Example: count_rises([3, 5, 4, 6]) → 2.
Off-by-one
count_digits(n) returns how many digits a positive whole number has. Example: count_digits(12345) → 5.
Type error
tickets_total(count_text, price) works out the cost of a booking. The ticket count comes from a form as text. Example: tickets_total("3", 20) → 60.
Type error
format_duration(seconds) shows a length as minutes:seconds, with two digits for seconds. Example: format_duration(125) → "2:05".
Other
to_fahrenheit(celsius) converts Celsius to Fahrenheit. Example: to_fahrenheit(100) → 212, to_fahrenheit(0) → 32.
Logic error
Showing 76–100 of 184 Python challenges