#2A: Function to implement AND-NOT using McCulloch-Pitts neuron
def and_not(x1,x2):
  w1 = 1 #weight for x1
  w2 = -1 #weight for x2
  threshold = 1  #threshold value
  #calculate the weighted sum
  weighted_sum = (x1*w1) + (x2*w2)
  #apply activation function
  if weighted_sum >= threshold:
    return 1 #neuron fires
  else:
    return 0 #neuron does not fires

#Display the truth table for AND-NOT
print("Truth Table for AND-NOT:")
print("x1 x2 | y")
print("----------")
for x1 in [0,1]:
  for x2 in [0,1]:
    y = and_not(x1,x2)
    print(f"{x1}  {x2}  | {y}")



#2B Generate XOR function using  McCulloch-Pitts neural net.

theta = 1

def output(w,X):
  return [int(x1 * w[0] + x2 * w[1] >= theta) for x1,x2 in X]

#Input set X
X = [(0,0),(0,1),(1,0),(1,1)]
print(f"X = {X}")

#Weight vector w1
w1 = [1, -1]
Y1 = output(w1, X)
print(f"Y1={Y1}")

#Weight vector w2
w2 = [-1, 1]
Y2 = output(w2, X)
print(f"Y2={Y2}")

#combine hidden layer outputs
Y = list(zip(Y1,Y2))
print(f"Y={Y}")

#Weight vector w3 for the combined input
w3 = [1,1]
Z = output(w3,Y)
print(f"Z={Z}")

