-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_exercises.py
More file actions
75 lines (60 loc) · 2.72 KB
/
Copy pathfunctions_exercises.py
File metadata and controls
75 lines (60 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# ==========================================
# Python Learning Lab: 03 Functions
# ==========================================
print("--- Running Functions Exercises ---")
# ==========================================
# Challenge 1: Variadic Arguments (*args and **kwargs)
# ==========================================
# Write a function `multiply_and_describe` that:
# 1. Multiplies all numbers passed as positional arguments (*args). If no numbers are passed, return 0.
# 2. Prints or formats key-value descriptive attributes (**kwargs) into a string.
# The function should return a tuple: (multiplication_result, description_dict)
def multiply_and_describe(*args, **kwargs):
# TODO: Implement multiplication and kwargs processing
if not args:
result = 0
else:
result = 1
for num in args:
result *= num
return (result, kwargs)
# --- Verification ---
res, desc = multiply_and_describe(2, 3, 4, item="widget", color="blue")
assert res == 24, f"Expected product 24, got {res}"
assert desc == {"item": "widget", "color": "blue"}, "Description dictionary does not match kwargs"
res_empty, desc_empty = multiply_and_describe()
assert res_empty == 0, f"Expected product 0 for no arguments, got {res_empty}"
print("✓ Challenge 1 passed!")
# ==========================================
# Challenge 2: Lambda and Map/Filter
# ==========================================
# Use a lambda function with:
# - `filter()` to extract words that start with "p" (case-insensitive).
# - `map()` to convert the filtered words to UPPERCASE.
# Return the final result as a list.
def process_words(word_list):
# TODO: Combine filter, map, list, and lambda
return list(map(lambda w: w.upper(), filter(lambda w: w.lower().startswith('p'), word_list)))
# --- Verification ---
words = ["python", "java", "Php", "rust", "Perl", "go"]
expected = ["PYTHON", "PHP", "PERL"]
assert process_words(words) == expected, f"Expected {expected}, got {process_words(words)}"
print("✓ Challenge 2 passed!")
# ==========================================
# Challenge 3: Decorators
# ==========================================
# Create a decorator `double_result` that doubles the integer output of any function it decorates.
def double_result(func):
# TODO: Define the wrapper function
def wrapper(*args, **kwargs):
return func(*args, **kwargs) * 2
return wrapper
# Decorate this dummy function
@double_result
def add_nums(a, b):
return a + b
# --- Verification ---
assert add_nums(2, 3) == 10, f"Expected double of 5, which is 10. Got {add_nums(2, 3)}"
assert add_nums(10, 20) == 60, f"Expected double of 30, which is 60. Got {add_nums(10, 20)}"
print("✓ Challenge 3 passed!")
print("\n🎉 All tests passed successfully!")