# A simple circuit # # Authors: David Kauchak # DON'T DO THIS FOR THE ASSIGNMENT, INSTEAD # COPY THE STARTER CODE INTO YOUR ASSIGNMENT AND # ADD YOUR ADDITIONAL FUNCTIONALITY! execfile("assign5-starter.py") def and2_test(): # construct the nodes in0 = Node(0) in1 = Node(0) out = Node(0) # construct the gate AndGate2(in0, in1, out) print "in0".rjust(5) + "in1".rjust(5) + "out".rjust(5) for val in range(4): i = val % 2 # get the first bit j = (val / 2) % 2 # get the second bit in0.set_state(i) in1.set_state(j) print str(i).rjust(5) + str(j).rjust(5) + str(out.get_state()).rjust(5) # don't do it this way, but I wanted to show you how # a more descriptive way of what's going on def and2_test_verbose(): # construct the nodes in0 = Node(0) in1 = Node(0) out = Node(0) # construct the gate AndGate2(in0, in1, out) print "in0".rjust(5) + "in1".rjust(5) + "out".rjust(5) in0.set_state(0) in1.set_state(0) print "0".rjust(5) + "0".rjust(5) + str(out.get_state()).rjust(5) in0.set_state(1) in1.set_state(0) print "1".rjust(5) + "0".rjust(5) + str(out.get_state()).rjust(5) in0.set_state(0) in1.set_state(1) print "0".rjust(5) + "1".rjust(5) + str(out.get_state()).rjust(5) in0.set_state(1) in1.set_state(1) print "1".rjust(5) + "1".rjust(5) + str(out.get_state()).rjust(5) def and3_test(): inputs = [Node(0), Node(0), Node(0)] out = Node(0) # construct the gate AndGate(inputs, out) print "in0".rjust(5) + "in1".rjust(5) + "in2".rjust(5) + "out".rjust(5) for val in range(8): i = val % 2 # get the first bit j = (val / 2) % 2 # get the second bit k = (val / 4) % 2 # get the third bit inputs[0].set_state(i) inputs[1].set_state(j) inputs[2].set_state(k) print str(inputs[0].get_state()).rjust(5) + \ str(inputs[1].get_state()).rjust(5) + \ str(inputs[2].get_state()).rjust(5) + \ str(out.get_state()).rjust(5)