جواب «All Tracks: Rank the Risks» چالش 6 از جلسهی 77
سؤال
یک فهرست خطر برای پروژه. امتیاز هر خطر = likelihood × impact (هر دو ۱ تا ۳). تابع rank(risks) خطرها را از بیشترین امتیاز مرتب کند (در امتیاز برابر، به ترتیب الفبای اسم). برای هر خطر <score> <name> چاپ کن و اگر امتیاز 6 یا بیشتر است و راه کم کردن (fix) خالی است، <- needs a plan اضافه کن. خروجی:9 private data leaks <- needs a plan6 hallucinated answers6 unbalanced classes <- needs a plan2 slow on old laptops1 boring colours
راهنما
sorted(risks, key=lambda r: (-r[1] * r[2], r[0])). در حلقه: score = likelihood * impact؛ line = f"{score} {name}"؛ if score >= 6 and not fix: line += " <- needs a plan".
یک جواب درست
RISKS = [ # (name, likelihood 1-3, impact 1-3, how to reduce it)
("boring colours", 1, 1, ""),
("hallucinated answers", 3, 2, "show the source and check numbers"),
("private data leaks", 3, 3, ""),
("slow on old laptops", 2, 1, "use smaller images"),
("unbalanced classes", 2, 3, ""),
]
def rank(risks):
return sorted(risks, key=lambda r: (-r[1] * r[2], r[0]))
for name, likelihood, impact, fix in rank(RISKS):
score = likelihood * impact
line = f"{score} {name}"
if score >= 6 and not fix:
line += " <- needs a plan"
print(line)
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن