-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastructures_exercises.py
More file actions
104 lines (83 loc) · 3.88 KB
/
Copy pathdatastructures_exercises.py
File metadata and controls
104 lines (83 loc) · 3.88 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# ==========================================
# Python Learning Lab: 04 Data Structures
# ==========================================
from collections import Counter, defaultdict
print("--- Running Data Structures Exercises ---")
# ==========================================
# Challenge 1: List Slicing & Manipulation
# ==========================================
# Write a function that takes a list and returns:
# - A tuple of: (first_three_elements, last_two_elements, reversed_list)
def slice_operations(lst):
# TODO: Implement list slicing
first_three = lst[:3]
last_two = lst[-2:]
reversed_lst = lst[::-1]
return (first_three, last_two, reversed_lst)
# --- Verification ---
test_list = [10, 20, 30, 40, 50, 60]
f_three, l_two, rev = slice_operations(test_list)
assert f_three == [10, 20, 30], f"Expected [10, 20, 30], got {f_three}"
assert l_two == [50, 60], f"Expected [50, 60], got {l_two}"
assert rev == [60, 50, 40, 30, 20, 10], f"Expected reversed, got {rev}"
print("✓ Challenge 1 passed!")
# ==========================================
# Challenge 2: Dictionary Manipulation
# ==========================================
# Write a function that updates prices in a menu dictionary:
# 1. Apply a discount percentage (e.g. 10% -> factor 0.90) to all existing item values.
# 2. Add a new item to the menu.
# 3. Retrieve the price of a target item safely; if it doesn't exist, return a default price of 0.
def update_menu(menu, discount_percent, new_item_name, new_item_price, target_search):
# TODO: Modify menu dictionary, calculate discount, add new item, and retrieve target
discount_factor = 1.0 - (discount_percent / 100.0)
# Apply discount
for item in menu:
menu[item] = menu[item] * discount_factor
# Add new item
menu[new_item_name] = new_item_price
# Retrieve target price safely (return 0 if not found)
search_price = menu.get(target_search, 0)
return menu, search_price
# --- Verification ---
initial_menu = {"burger": 10.0, "pizza": 15.0}
updated_menu, searched = update_menu(initial_menu, 20, "fries", 5.0, "soda")
assert updated_menu["burger"] == 8.0, f"Burger price should be 8.0, got {updated_menu['burger']}"
assert updated_menu["pizza"] == 12.0, f"Pizza price should be 12.0, got {updated_menu['pizza']}"
assert updated_menu["fries"] == 5.0, f"Fries should be added at 5.0, got {updated_menu.get('fries')}"
assert searched == 0, f"Soda price search should return default 0, got {searched}"
print("✓ Challenge 2 passed!")
# ==========================================
# Challenge 3: Set Operations
# ==========================================
# Return a tuple containing:
# 1. Common elements in sets A and B (intersection).
# 2. Elements that are ONLY in set A (difference).
# 3. Unique elements across both sets combined (union).
def set_math(set_a, set_b):
# TODO: Perform set operations
intersection = set_a & set_b
difference = set_a - set_b
union = set_a | set_b
return (intersection, difference, union)
# --- Verification ---
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
inter, diff, uni = set_math(a, b)
assert inter == {3, 4}, f"Expected {3, 4}, got {inter}"
assert diff == {1, 2}, f"Expected {1, 2}, got {diff}"
assert uni == {1, 2, 3, 4, 5, 6}, f"Expected combined union, got {uni}"
print("✓ Challenge 3 passed!")
# ==========================================
# Challenge 4: Collections Module
# ==========================================
# Use Counter to count the frequency of characters in a string.
# Return the character that occurs most frequently.
def most_frequent_char(text):
# TODO: Use Counter to find the most common character
# Hint: Counter(text).most_common(1)
return Counter(text).most_common(1)[0][0]
# --- Verification ---
assert most_frequent_char("success") == "s", f"Expected 's', got {most_frequent_char('success')}"
print("✓ Challenge 4 passed!")
print("\n🎉 All tests passed successfully!")