🐍 Python for Everyone

← Back to practice bank

Chapter 9 Coding Challenges 50-65 min, auto-graded

Coding Challenges: Comprehensions, Lambda & Functional Tools

8 HackerRank-style challenges, auto-graded right in your browser — no setup required. Write your function in the code box, then click Run Tests to check it against a set of test cases (including edge cases like empty lists and dicts). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-9 have covered so far — list, dict, and set comprehensions, nested comprehensions, lambda, and map()/filter()/sorted(), plus the function stub already given (no def beyond that stub — you haven't learned to write your own yet).

How grading works Your code runs entirely in your browser using Pyodide (Python compiled to WebAssembly) — nothing is uploaded anywhere. Keep the function name in the stub exactly as given, since the grader calls it by name.

1 Squares of Even Numbers Easy

Write a function squares_of_evens(numbers) that returns a list of the squares of only the even numbers in numbers, using a single list comprehension.

Constraints: numbers may be empty (return []); numbers may be negative; preserve the original order.
Examples:
squares_of_evens([1, 2, 3, 4, 5, 6]) → [4, 16, 36]
squares_of_evens([]) → []
squares_of_evens([1, 3, 5]) → []
challenge_1.py
Show Solution
solution_1.py
def squares_of_evens(numbers):
    return [n * n for n in numbers if n % 2 == 0]

2 Build a Lookup Dict From Two Lists Easy

Write a function build_lookup(keys, values) that returns a dictionary mapping each element of keys to the element at the same position in values, using a dict comprehension over range(len(keys)).

Constraints: keys and values are always the same length; both may be empty (return {}).
Examples:
build_lookup(["a", "b", "c"], [1, 2, 3]) → {"a": 1, "b": 2, "c": 3}
build_lookup([], []) → {}
build_lookup(["x"], [100]) → {"x": 100}
challenge_2.py
Show Solution
solution_2.py
def build_lookup(keys, values):
    return {keys[i]: values[i] for i in range(len(keys))}

3 Deduplicate and Sort Easy

Write a function dedupe_sorted(items) that removes duplicates from items using a set comprehension, then returns the unique values as a sorted list.

Constraints: items may be empty (return []); items are always mutually comparable (all strings or all numbers).
Examples:
dedupe_sorted(["b", "a", "b", "c", "a"]) → ["a", "b", "c"]
dedupe_sorted([]) → []
dedupe_sorted([3, 1, 2, 1, 3]) → [1, 2, 3]
challenge_3.py
Show Solution
solution_3.py
def dedupe_sorted(items):
    return sorted({item for item in items})

4 Flatten a Nested List Easy

Write a function flatten(nested) that takes a list of lists and returns one flat list containing every inner value, in order, using a nested list comprehension.

Constraints: nested may be empty, or may contain empty inner lists; values keep their original order.
Examples:
flatten([[1, 2], [3], [4, 5, 6]]) → [1, 2, 3, 4, 5, 6]
flatten([]) → []
flatten([[], [1, 2], []]) → [1, 2]
challenge_4.py
Show Solution
solution_4.py
def flatten(nested):
    return [value for row in nested for value in row]

5 Filter Active Records Medium

Write a function filter_active(records) that takes a list of dictionaries, each with an "active" key, and returns a new list containing only the records where "active" is True, using filter() with a lambda.

Constraints: records may be empty (return []); every record is guaranteed to have an "active" key; preserve original order.
Examples:
filter_active([{"name": "a", "active": True}, {"name": "b", "active": False}]) → [{"name": "a", "active": True}]
filter_active([]) → []
challenge_5.py
Show Solution
solution_5.py
def filter_active(records):
    return list(filter(lambda r: r["active"], records))

6 Sort Records By a Field Medium

Write a function sort_by_field(records, field) that returns a new list of dictionaries sorted by the value at record[field], ascending, using sorted() with key=lambda.

Constraints: records may be empty (return []); every record is guaranteed to contain field; values at field are always mutually comparable.
Examples:
sort_by_field([{"name": "b", "age": 30}, {"name": "a", "age": 20}], "age") → [{"name": "a", "age": 20}, {"name": "b", "age": 30}]
sort_by_field([], "age") → []
challenge_6.py
Show Solution
solution_6.py
def sort_by_field(records, field):
    return sorted(records, key=lambda r: r[field])

7 Transform a List of Strings Medium

Write a function shout_all(words) that returns a new list where each word in words has been uppercased and had "!" appended, using map() with a lambda.

Constraints: words may be empty (return []); preserve original order.
Examples:
shout_all(["hi", "bye"]) → ["HI!", "BYE!"]
shout_all([]) → []
shout_all(["a"]) → ["A!"]
challenge_7.py
Show Solution
solution_7.py
def shout_all(words):
    return list(map(lambda w: w.upper() + "!", words))

8 Clean and Rank Raw Scores Medium

Write a function clean_and_rank_scores(raw_scores) that takes a list of raw strings (some may have stray whitespace, some may not be numbers at all). Keep only the ones that are valid whole numbers after stripping whitespace, convert them to int, and return them sorted from highest to lowest. Combine filter() with a lambda, a list comprehension, and sorted() in one small pipeline.

Constraints: raw_scores may be empty (return []); non-numeric strings (like "abc") must be dropped, not crash the function; whitespace around a valid number should be ignored.
Examples:
clean_and_rank_scores([" 88", "abc", "92 ", " 75 ", "60", "xx"]) → [92, 88, 75, 60]
clean_and_rank_scores([]) → []
clean_and_rank_scores(["10"]) → [10]
challenge_8.py
Show Solution
solution_8.py
def clean_and_rank_scores(raw_scores):
    numeric = filter(lambda s: s.strip().isdigit(), raw_scores)
    cleaned = [int(s.strip()) for s in numeric]
    return sorted(cleaned, reverse=True)
← Practice Bank Interview Questions →