We can show conditions for AND, OR by simple codes:
def funAnd():
if condition1 and condition2:
return True
#######
0 | 0 -> 0
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1
def funOr():
if condition1 or condition2:
return True
#######
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
#######################################
what about generating conditional tests for XOR, NAND, NOR, XNOR logics.
########## XOR
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
########## NAND
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
########## NOR
0 | 0 -> 1
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 0
########## XNOR
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
How can we test for these logic?
writing code for logic conditions
Page 1 of 14 Replies - 357 Views - Last Post: 07 February 2012 - 11:55 AM
Topic Sponsor:
Replies To: writing code for logic conditions
#2
Re: writing code for logic conditions
Posted 07 February 2012 - 06:23 AM
By rewriting them in terms of not, and and or. For example x nor y can be trivially expressed as not(x or y).
#3
Re: writing code for logic conditions
Posted 07 February 2012 - 08:02 AM
thanks sepp2K.
what about other logics??
what about other logics??
#4
Re: writing code for logic conditions
Posted 07 February 2012 - 08:28 AM
First your truth table for xor and xnor appear incorrect you have:
It should be:
Jim
Quote
########## XOR
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
########## XNOR
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
########## XNOR
0 | 0 -> 1
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
It should be:
Quote
########## XOR
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
########## XNOR
0 | 0 -> 1
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 0
########## XNOR
0 | 0 -> 1
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1
Jim
#5
Re: writing code for logic conditions
Posted 07 February 2012 - 11:55 AM
No code? Sad.
Here's a simple tester:
Here's a simple example:
Have at!
Here's a simple tester:
def showTable(symbol, func):
print(symbol)
for c1 in (0,1):
for c2 in (0,1):
result = int(func(c1==1, c2==1))
print('{0} | {1} -> {2}'.format(c1, c2, result))
Here's a simple example:
>>> showTable('AND', lambda c1, c2: c1 and c2)
AND
0 | 0 -> 0
0 | 1 -> 0
1 | 0 -> 0
1 | 1 -> 1
>>>
Have at!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|