بتل کد Battle Codeدارد باز می‌شود…

جواب «Read the Tool Call» چالش 2 از جلسه‌ی 43

سؤال

مدل وقتی ابزار می‌خواهد جوابش این شکل است: TOOL calc 1234 * 5678. parse_tool(reply):
- فاصله‌های دو سر را بردار.
- اگر با TOOL شروع نشد، None.
- با reply.split(" ", 2) به سه تکه بشکن (فقط دو بار بشکن، تا ورودی سالم بماند). اگر کم‌تر از سه تکه شد، None؛ وگرنه (name, arg).
خروجی test:
('calc', '1234 * 5678')
('shout', 'hello there')
None
None

راهنما

reply.startswith("TOOL ") و parts = reply.split(" ", 2)؛ بعد return (parts[1], parts[2]).

یک جواب درست

def parse_tool(reply):
    reply = reply.strip()
    if not reply.startswith("TOOL "):
        return None
    parts = reply.split(" ", 2)
    if len(parts) < 3:
        return None
    return (parts[1], parts[2])


# ---- test (do not change) ----
print(parse_tool("TOOL calc 1234 * 5678"))
print(parse_tool("  TOOL shout hello there  "))
print(parse_tool("The answer is 7."))
print(parse_tool("TOOL calc"))

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن