Midterm Review: Python Problem Sets 4
-
Write a function that sorts a list of tuples by their second element in ascending order.
def sort_by_second(data: list[tuple]) -> list[tuple]: # Your code here
Example usage:
data = [("a", 3), ("b", 1), ("c", 2)] print(sort_by_second(data)) # Output: [("b", 1), ("c", 2), ("a", 3)]
-
Create a list comprehension to generate all numbers divisible by 3 in a given range eg. 1 to 100 inclusive.
def generate_divisible_by_3(lowerbound: int, upperbound: int) -> list[int]: # Your return statement here
Example usage:
print(generate_divisible_by_3(1, 12)) # Output: [3, 6, 9, 12]
-
Write a function that extracts and reverses a slice of a list (start to end indices inclusive). It should handle out-of-bound cases gracefully. You are not allowed to use the built-in slice operations.
def reverse_slice(lst: list[int], start: int, end: int) -> list[int]: # Your code here
Example usage:
lst = [10, 20, 30, 40, 50] print(reverse_slice(lst, 1, 3)) # Output: [40, 30, 20] print(reverse_slice(lst, 2, 8)) # Output: [50, 40, 30]
-
Write a Python function
apply_transformation
that accepts a list of integers and a callback function.apply_transformation
should return a new list where each element is transformed according to the callback function. The callback function itself should be one that accepts an integer value and returns the transformed value.Your function should have the following signature:
def apply_transformation(numbers: list[int], transform_function: Callable) -> list: # Your code here
Example usage:
my_list = [1, 2, 3] number_doubler_lambda = ... # create your lambda here number_squarer_lambda = ... # create your lambda here doubled_nums = apply_transformation(my_list, number_doubler_lambda) squared_nums = apply_transformation(my_list, number_squarer_lambda) print(doubled_nums) # outputs: [2, 4, 6] print(squared_nums) # outputs: [1, 4, 9]
-
Write an expression that sorts a list of integers based on their right-most digit.
Example:
mylist = [5, 4, 21, 20, 39] # Your expression here print(mylist) # output: [20, 21, 4, 5, 39]
-
What is the output of this code, and why?
def func(x=[1,2,3]): x.append(4) return x print(func()) print(func([5,6,7])) print(func())
-
What happens when you run this code?
lst = [1, 2, 3, 4, 5] lst[1:4:2] = [10, 20] print(lst)
-
What is the output of this code?
d = [4, 1, 6] lst = [*d, *sorted(d)] print(lst)
-
What is printed and why?
lst = [1, 2, 3, 4, 5] new_lst = lst[::-1][:3][::-1] print(new_lst)
-
What is the output of this code?
def create_multipliers(): multipliers = [] for i in range(3): def multiplier(x, i=i): return x * i multipliers.append(multiplier) return multipliers funcs = create_multipliers() print([f(2) for f in funcs])
-
What does this code print?
items = [(4, 444), (1, 11), (3, 3333)] sorted_items = sorted(sorted(items, key=lambda x: x[1]), key=lambda x: x[0]) print(sorted_items)
-
What’s the output of this code?
items = [11, 22, 33, 44, 55] counts = [1, 2, 3, 4, 5] result = [(items[i], counts[i]) for i in range(len(sorted(items, reverse=True)))] print(result)
-
What’s printed and why?
def func(items, value): items.append(value) return items numbers = [1, 2, 3] print(func(numbers[:], 4)) print(numbers)
-
What’s printed?
mylist = [1, 2, 3, 4, 5] mylist[1:4] = sorted(mylist[1:4], reverse=True) print(mylist)
-
What’s the output of this code?
nums1 = list(range(5)) nums2 = sorted(nums1, reverse=True)[::-1] print(nums1) print(nums2)