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

جواب «Check Against the Truth» چالش 4 از جلسه‌ی 9

سؤال

لیست truth جدول درستِ AND را دارد. برای وزن‌های w1, w2 و آستانه‌ی داده‌شده، بشمار نورون چند سطر را درست جواب می‌دهد و چاپ کن correct: 4 of 4. (اول با وزن‌های 1, 1, 2، بعد با 1, 1, 1.)

راهنما

for x1, x2, want in truth: if neuron(x1, x2, w1, w2, threshold) == want: correct += 1.

یک جواب درست

def neuron(x1, x2, w1, w2, threshold):
    total = x1 * w1 + x2 * w2
    if total >= threshold:
        return 1
    return 0

truth = [(0, 0, 0), (0, 1, 0), (1, 0, 0), (1, 1, 1)]

def score(w1, w2, threshold):
    correct = 0
    for x1, x2, want in truth:
        if neuron(x1, x2, w1, w2, threshold) == want:
            correct += 1
    return correct

print("correct:", score(1, 1, 2), "of 4")
print("correct:", score(1, 1, 1), "of 4")

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