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

جواب «A Neuron Function» چالش 3 از جلسه‌ی 30

سؤال

تابع neuron(inputs, weights, threshold) را بنویس: جمع inputs[i] * weights[i] را برای همه‌ی i حساب کن و اگر به threshold رسید 1، وگرنه 0 برگردان. خروجی:
[1, 1, 0] -> 1
[1, 0, 1] -> 0
[0, 1, 1] -> 1
[0, 0, 1] -> 0

راهنما

total = 0، for i in range(len(inputs)): total += inputs[i] * weights[i]، بعد if total >= threshold: return 1.

یک جواب درست

def neuron(inputs, weights, threshold):
    total = 0
    for i in range(len(inputs)):
        total += inputs[i] * weights[i]
    if total >= threshold:
        return 1
    return 0

cases = [[1, 1, 0], [1, 0, 1], [0, 1, 1], [0, 0, 1]]
for inputs in cases:
    print(inputs, "->", neuron(inputs, [2, 3, 1], 4))

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