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

جواب «The Biggest Risks First» چالش 5 از جلسه‌ی 46

سؤال

هر خطر یک تاپل است: (name, chance, harm, control) با chance و harm از ۱ تا ۳. top_risks(risks, n) خطرها را از بیشترین chance * harm به کمترین چاپ کند (برابرها به ترتیب لیست) و فقط n تای اول را: 9 prompt injection -> spot attacks, fence the text, check the output. بدون sort: امتیاز را از 9 تا 1 پایین بیا و در هر امتیاز خطرهای همان امتیاز را چاپ کن. خروجی test:
9 prompt injection -> spot attacks, fence the text, check the output
6 it makes up answers -> answer only from the handout and show the source
6 someone types their phone number -> warn users and never save the chat

راهنما

for level in range(9, 0, -1): و داخلش for name, chance, harm, control in risks:. یک شمارنده‌ی shown نگه دار و وقتی به n رسید return.

یک جواب درست

def top_risks(risks, n):
    shown = 0
    for level in range(9, 0, -1):
        for name, chance, harm, control in risks:
            if chance * harm == level:
                print(level, name, "->", control)
                shown += 1
                if shown == n:
                    return


# ---- test (do not change) ----
risks = [
    ("it makes up answers", 3, 2, "answer only from the handout and show the source"),
    ("someone types their phone number", 2, 3, "warn users and never save the chat"),
    ("the relay is off", 2, 1, "use a ready answer"),
    ("prompt injection", 3, 3, "spot attacks, fence the text, check the output"),
    ("it is too slow", 1, 1, "keep the prompt short"),
]
top_risks(risks, 3)

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