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).
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.
numbers may be empty (return []); numbers may be negative; preserve the original order.
squares_of_evens([1, 2, 3, 4, 5, 6]) → [4, 16, 36]squares_of_evens([]) → []squares_of_evens([1, 3, 5]) → []
Show Solution
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)).
keys and values are always the same length; both may be empty (return {}).
build_lookup(["a", "b", "c"], [1, 2, 3]) → {"a": 1, "b": 2, "c": 3}build_lookup([], []) → {}build_lookup(["x"], [100]) → {"x": 100}
Show Solution
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.
items may be empty (return []); items are always mutually comparable (all strings or all numbers).
dedupe_sorted(["b", "a", "b", "c", "a"]) → ["a", "b", "c"]dedupe_sorted([]) → []dedupe_sorted([3, 1, 2, 1, 3]) → [1, 2, 3]
Show Solution
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.
nested may be empty, or may contain empty inner lists; values keep their original order.
flatten([[1, 2], [3], [4, 5, 6]]) → [1, 2, 3, 4, 5, 6]flatten([]) → []flatten([[], [1, 2], []]) → [1, 2]
Show Solution
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.
records may be empty (return []); every record is guaranteed to have an "active" key; preserve original order.
filter_active([{"name": "a", "active": True}, {"name": "b", "active": False}]) → [{"name": "a", "active": True}]filter_active([]) → []
Show Solution
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.
records may be empty (return []); every record is guaranteed to contain field; values at field are always mutually comparable.
sort_by_field([{"name": "b", "age": 30}, {"name": "a", "age": 20}], "age") → [{"name": "a", "age": 20}, {"name": "b", "age": 30}]sort_by_field([], "age") → []
Show Solution
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.
words may be empty (return []); preserve original order.
shout_all(["hi", "bye"]) → ["HI!", "BYE!"]shout_all([]) → []shout_all(["a"]) → ["A!"]
Show Solution
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.
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.
clean_and_rank_scores([" 88", "abc", "92 ", " 75 ", "60", "xx"]) → [92, 88, 75, 60]clean_and_rank_scores([]) → []clean_and_rank_scores(["10"]) → [10]
Show Solution
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)