forked from micanipho/Python-assessment-002
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.py
More file actions
157 lines (124 loc) · 4.75 KB
/
analytics.py
File metadata and controls
157 lines (124 loc) · 4.75 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# ==========================================
# SECTION A: LIST PROCESSING
# ==========================================
def filter_sales_above_threshold(sales: list, threshold: int):
"""
QUESTION 1
----------------------------------------
Given a list of sales amounts (integers), return a NEW list containing
only values above the threshold.
Example: filter_sales_above_threshold([100, 250, 75, 300], 150) → [250, 300]
Logic:
- Do NOT modify the original list
- Return empty list if no values qualify
- Use a loop or list comprehension
"""
# # TODO: Write your code here
# pass
list=[]
for i in list:
if i>threshold:
return i
else:
return list
# print(filter_sales_above_threshold(([100, 250, 75, 300], 150)))
def count_product_codes(codes: list, prefix: str):
"""
QUESTION 2
----------------------------------------
Given a list of product codes (strings) and a prefix (string),
count how many codes START with the given prefix (case-sensitive).
Example: count_product_codes(["PROD-001", "PROD-002", "SERV-001"], "PROD") → 2
Logic:
- Return 0 if no matches found
"""
# TODO: Write your code here
pass
count = 0
for code in codes:
if code.startswith(prefix):
count += 1
return count
def calculate_moving_average(numbers: list, window_size: int):
"""
QUESTION 3
----------------------------------------
Calculate the average of the LAST window_size elements in the list.
Example: calculate_moving_average([10, 20, 30, 40, 50], 3) → 40.0
(average of last 3: 30, 40, 50)
Logic:
- If the list has fewer elements than window_size, use all available elements
- Return the average as a float rounded to 2 decimal places
- Return 0.0 for empty list
"""
# # TODO: Write your code here
# pass
total=sum(numbers)
result=total/window_size
return result
# print(calculate_moving_average([10, 20, 30, 40, 50], 3))
# ==========================================
# SECTION B: DICTIONARY OPERATIONS
# ==========================================
def get_top_seller(sales_data: dict):
"""
QUESTION 4
----------------------------------------
Given a dictionary where keys are employee names and values are sales totals,
return the NAME of the employee with the highest sales.
Example: {"Alice": 5000, "Bob": 7500, "Carol": 6000} → "Bob"
Logic:
- If dictionary is empty, return "No Data"
- If there's a tie, return the name that appears first alphabetically
"""
# TODO: Write your code here
pass
def merge_inventory(warehouse_a: dict, warehouse_b: dict):
"""
QUESTION 5
----------------------------------------
Given two dictionaries representing product quantities in two warehouses,
return a NEW dictionary with combined quantities for each product.
Example:
warehouse_a = {"PROD-001": 50, "PROD-002": 30}
warehouse_b = {"PROD-002": 20, "PROD-003": 40}
Result: {"PROD-001": 50, "PROD-002": 50, "PROD-003": 40}
Logic:
- If a product exists in only one warehouse, include it with its quantity
- Return empty dictionary if both inputs are empty
- Do NOT modify the original dictionaries
"""
# TODO: Write your code here
pass
# ==========================================
# SECTION C: COMPLEX LOGIC & TDD
# ==========================================
def check_inventory_status(stock_level: int, reorder_point: int, max_capacity: int, daily_sales: int):
"""
QUESTION 6 (THE INVENTORY MANAGER)
----------------------------------------
Analyze inventory status based on multiple parameters.
TODO: Using TDD, implement tests for the below functionality.
Create `test_inventory.py` with class `TestInventory`.
Parameters:
- stock_level: Current units in stock
- reorder_point: Threshold that triggers reorder
- max_capacity: Maximum warehouse capacity
- daily_sales: Average units sold per day
Logic Tree:
1. INPUT VALIDATION:
- If ANY parameter is negative: return "Invalid Input"
- If stock_level > max_capacity: return "Invalid Input"
2. OVERSTOCKED:
- If stock_level > (max_capacity * 0.9): return "OVERSTOCKED"
3. CRITICAL (Urgent Reorder):
- If stock_level < (reorder_point * 0.5): return "CRITICAL"
4. REORDER NEEDED:
- If stock_level <= reorder_point: return "REORDER"
5. LOW STOCK (Warning):
- If daily_sales > 0 AND stock_level / daily_sales < 7: return "LOW STOCK"
6. OPTIMAL:
- All other cases: return "OPTIMAL"
"""
# TODO: Write your code here
pass