-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
72 lines (60 loc) · 2.28 KB
/
Copy pathexample.py
File metadata and controls
72 lines (60 loc) · 2.28 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
"""Example script."""
def main() -> int:
"""Main function."""
order_totals = []
for order_id in range(120):
item_count = 12 + (order_id % 9)
subtotal = estimate_order_subtotal(order_id, item_count)
route_score = estimate_delivery_route(order_id, zone_count=7)
order_totals.append(subtotal + route_score)
return sum(order_totals)
def estimate_order_subtotal(order_id: int, item_count: int) -> int:
"""Estimate the subtotal of an order."""
subtotal = 0
for item_idx in range(item_count):
base_price = 15 + ((order_id * 3 + item_idx * 5) % 45)
quantity = 1 + ((order_id + item_idx) % 4)
discount = (item_idx % 3) * 2
line_total = max(5, base_price - discount) * quantity
subtotal += line_total
if line_total > 120:
subtotal -= 3
return subtotal
def calculate_congestion_penalty(order_id: int, step: int, next_zone: int) -> int:
"""Calculate the congestion penalty for an order."""
penalty = 0
for minute in range(5):
wave = (step + minute + order_id) % 11
penalty += (wave * (next_zone + 1)) % 13
return penalty
def estimate_delivery_route(order_id: int, zone_count: int) -> int:
"""Estimate the delivery route for an order."""
score = 0
last_zone = order_id % zone_count
for step in range(300):
next_zone = (last_zone * 2 + step + order_id) % zone_count
traffic_factor = 1 + ((step + order_id) % 5)
zone_distance = 8 + (next_zone * 3 + step % 4)
score += zone_distance * traffic_factor
score += calculate_congestion_penalty(order_id, step, next_zone)
if next_zone == last_zone:
score += 6
else:
score -= 1
last_zone = next_zone
return score
if __name__ == "__main__":
PROFILE = True
PROFILE_DIR = "profile"
if PROFILE:
from line_profiler import LineProfiler
from line_profiler_web import save_stats
lp = LineProfiler()
lp.add_function(main)
lp.add_function(estimate_order_subtotal)
lp.add_function(estimate_delivery_route)
lp.add_function(calculate_congestion_penalty)
lp.run("main()") # type: ignore[no-untyped-call]
save_stats(lp, PROFILE_DIR)
else:
main()