高级农民
- 积分
- 1201
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-12-20
- 最后登录
- 1970-1-1
|
- import json
- import time
- import random
- # Constants
- INGESTION_RATE = 2 # orders per second
- COURIER_ARRIVAL_RANGE = (2, 6) # seconds
- SHELVES = {
- "Hot": {"temp": "hot", "capacity": 10, "decay_modifier": 1},
- "Cold": {"temp": "cold", "capacity": 10, "decay_modifier": 1},
- "Frozen": {"temp": "frozen", "capacity": 10, "decay_modifier": 1},
- "Overflow": {"temp": "any", "capacity": 15, "decay_modifier": 2}
- }
- # Load orders from JSON file
- with open("orders.json") as f:
- orders = json.load(f)
- # Initialize the shelves
- shelves = {name: [] for name in SHELVES.keys()}
- # Process orders
- for order in orders:
- # Wait until the next ingestion time
- time.sleep(1 / INGESTION_RATE)
- # Cook the order
- order["age"] = 0 # initialize age of order
- # Place the order on the appropriate shelf
- shelf_name = None
- for name, shelf in SHELVES.items():
- if shelf["temp"] == order["temp"]:
- shelf_name = name
- break
- # If the shelf is full, place the order on the overflow shelf
- if len(shelves[shelf_name]) == SHELVES[shelf_name]["capacity"]:
- shelf_name = "Overflow"
- # Add the order to the shelf
- shelves[shelf_name].append(order)
- print(f"Placed order {order['id']} on {shelf_name} shelf")
- # Dispatch a courier to pick up the order
- courier_arrival_time = random.uniform(*COURIER_ARRIVAL_RANGE)
- time.sleep(courier_arrival_time)
- # Courier arrives and picks up the order
- print(f"Courier picked up order {order['id']} from {shelf_name} shelf")
- # Courier delivers the order
- print(f"Courier delivered order {order['id']}")
- # All orders have been processed
- print("All orders have been fulfilled")
复制代码 |
|