← 返回题库
中级

实现按设备类型差异化限流

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    import pandas as pd
    logs_csv = open_url(____).read()
    cfg_csv = open_url(____).read()
    logs = pd.read_csv(StringIO(____))
    cfg = pd.read_csv(StringIO(____))
    limits = {row['____']:(____) for _,row in cfg.iterrows()}
    def check_limit(____):
        max_req, window = limits.get(device_type, limits.get('____',(____,____)))
        sorted_ts = sorted(____)
        violations = ____
        for i, ts in enumerate(____):
            window_start = ts - window
            in_window = sum(____ for t in sorted_ts[:i+____] if t >= window_start)
            if in_window > max_req: violations += ____
        return violations
    print(____)
    for (____), grp in logs.groupby(____):
        v = check_limit(did, dtype, list(____))
        max_req, win = limits.get(dtype, (____,____))
        print("____" + did + "____" + dtype + "____" + str(____) + "____" + str(____) + "____" + str(____))

示例

输入
solve()
期望输出
Rate limit by device type:
  ARD-001(arduino) limit=1/1s: violations=19
  ARD-002(arduino) limit=1/1s: violations=0
  ESP-001(esp32) limit=2/1s: violations=0
  GW-001(gateway) limit=10/1s: violations=0
  RPI-001(raspberry_pi) limit=5/1s: violations=0
  RPI-002(raspberry_pi) limit=5/1s: violations=0
  RPI-003(raspberry_pi) limit=5/1s: violations=0
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码