Data Structures Assessment
Test your mastery of Python data structures: lists, tuples, dictionaries, sets, comprehensions, slicing, sorting, and nested structures.
Write a function flatten(nested) that takes a list which may contain nested lists (one level deep) and returns a single flat list of all elements in order.
flatten([[1, 2], [3], [4, 5, 6]]) # [1, 2, 3, 4, 5, 6]
flatten([["a"], ["b", "c"]]) # ["a", "b", "c"]Write a function word_freq(text) that takes a string of space-separated words and returns a dictionary mapping each word (lowercased) to its count.
word_freq("the cat and the dog") # {"the": 2, "cat": 1, "and": 1, "dog": 1}The function transpose(matrix) should return the transpose of a 2D list (swap rows and columns). It has a bug -- find and fix it.
transpose([[1, 2, 3], [4, 5, 6]]) # [[1, 4], [2, 5], [3, 6]]What does the following code print?
nums = [10, 20, 30, 40, 50, 60, 70]
print(nums[1:5:2])
print(nums[::-2])Write a program that prints the exact same output.
Write a function unique(items) that returns a new list with duplicates removed, preserving the order of first appearance.
unique([3, 1, 4, 1, 5, 9, 2, 6, 5, 3]) # [3, 1, 4, 5, 9, 2, 6]The function merge_dicts(a, b) should return a new dictionary containing all keys from both a and b. If a key exists in both, the values should be summed. It has a bug -- find and fix it.
merge_dicts({"x": 1, "y": 2}, {"y": 3, "z": 4})
# {"x": 1, "y": 5, "z": 4}What does the following code print?
data = [("Alice", 90), ("Bob", 75), ("Carol", 90), ("Dave", 80)]
sorted_data = sorted(data, key=lambda t: (-t[1], t[0]))
for name, score in sorted_data:
print(f"{name}: {score}")Write a program that produces the exact same output.
Refactor the function square_map(nums) to use a dictionary comprehension instead of a loop. The function should return a dict mapping each number to its square.
The refactored function must produce identical results and use a dict comprehension (no loops).
square_map([1, 2, 3]) # {1: 1, 2: 4, 3: 9}The function common_and_unique(list_a, list_b) should return a tuple of two sorted lists: elements common to both lists, and elements that appear in only one list (symmetric difference). It has a bug -- find and fix it.
common_and_unique([1, 2, 3, 4], [3, 4, 5, 6])
# ([3, 4], [1, 2, 5, 6])What does the following code print?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
result = [matrix[i][i] for i in range(len(matrix))]
print(result)
flat_even = [x for row in matrix for x in row if x % 2 == 0]
print(flat_even)Write a program that produces the exact same output.
Refactor the function get_top_scorers(students) to use a list comprehension instead of the loop. The function takes a list of {"name": str, "scores": list[int]} dicts and returns a sorted list of names of students whose average score is 80 or above.
The refactored function must produce identical results and use a list comprehension.
Write a function group_by(items, key_func) that groups a list of items into a dictionary based on the result of calling key_func on each item. Each key maps to a list of items that produced that key.
group_by(["hi", "hey", "bye", "be"], len)
# {2: ["hi", "be"], 3: ["hey", "bye"]}