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

Step 3: The Incident Report چالش 6 از جلسه‌ی 30، سخت

(سخت‌تر) قدم ۳: بعد از خروجی قدم ۲، تعداد رویدادهایی را که fail دارند در fails بشمار و گزارش را چاپ کن. همه‌ی عددها و زمان‌ها باید از متغیرها بیایند (fails، start[1]، fix[1]، gap، harm، gems)، نه دستی. خطهای تازه‌ی خروجی:
INCIDENT REPORT
what: 2 failed logins, then mina's account was opened from a new device
when: 16:58:12 to 17:20:00, 21 min 48 s
harm: 3 changes, 750 gems given away
fix: password reset and all sessions logged out
lesson: a unique long password and two-step login

def seconds(t):
    h, m, s = t.split(":")
    return int(h) * 3600 + int(m) * 60 + int(s)

router = [
    ("16:40:03", "known device LAP-12 joined class Wi-Fi"),
    ("16:58:12", "new device TAB-31 joined guest Wi-Fi"),
]
server = [
    ("16:45:00", "login ok user=sara from LAP-12"),
    ("16:59:01", "login fail user=mina from TAB-31"),
    ("16:59:20", "login fail user=mina from TAB-31"),
    ("16:59:44", "login ok user=mina from TAB-31 new device"),
    ("17:20:00", "admin reset password user=mina"),
    ("17:21:30", "admin logged out all sessions user=mina"),
]
app = [
    ("17:02:05", "mina changed recovery email"),
    ("17:03:40", "mina gave away 500 gems"),
    ("17:04:10", "mina gave away 250 gems"),
]

timeline = []
for t, text in router:
    timeline.append((seconds(t), t, "router", text))
for t, text in server:
    timeline.append((seconds(t), t, "server", text))
for t, text in app:
    timeline.append((seconds(t), t, "app", text))
events = sorted(timeline)
for e in events:
    print(e[1], e[2], e[3])
print("events:", len(events))

words = ["fail", "new device"]
start = None
fix = None
harm = 0
gems = 0
for e in events:
    text = e[3]
    for w in words:
        if w in text and start is None:
            start = e
    if "admin" in text and fix is None:
        fix = e
    if "changed" in text or "gave away" in text:
        harm = harm + 1
    if "gave away" in text:
        gems = gems + int(text.split()[3])
gap = fix[0] - start[0]
print("start:", start[1])
print(f"harm: {harm} events, {gems} gems")
print(f"fixed: {fix[1]} ({gap // 60} min {gap % 60} s after the start)")

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 30: مرور ۴ + پروژه: گزارش حادثه

بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال