Difference between revisions of "CSC270 GenerateTruthTable.py"
Line 1: | Line 1: | ||
One can use python (or any other language) to easily generate truth tables. This is a simple way to test a hypothesis, or to verify special cases in design situations. Don't hesitate to use this approach to save time and generate accurate results. | One can use python (or any other language) to easily generate truth tables. This is a simple way to test a hypothesis, or to verify special cases in design situations. Don't hesitate to use this approach to save time and generate accurate results. | ||
− | < | + | ::<source lang="python"> |
# truthtable.py | # truthtable.py | ||
Line 42: | Line 42: | ||
− | </ | + | </source> |
The output is show below: | The output is show below: | ||
− | < | + | ::<source lang="text"> |
a b c | f g h | a b c | f g h | ||
Line 61: | Line 61: | ||
− | </ | + | </source> |
Revision as of 12:26, 12 January 2016
One can use python (or any other language) to easily generate truth tables. This is a simple way to test a hypothesis, or to verify special cases in design situations. Don't hesitate to use this approach to save time and generate accurate results.
# truthtable.py # D. Thiebaut # how a simple python program can generate the # truth table of a boolean function # # 3 functions of 3 variables are shown here: # _ # f = a.b + c # _ _ _ # g = a + b + c # _ # h = a + b def f( a, b, c ): return ( a and (not b) ) or c def g( a, b, c ): return (not a) or (not b) or (not c) def h( a, b, c ): return a or not b def main(): print( " a b c | f g h" ) print( "-----------+---------" ) for a in [ 0, 1 ]: for b in [ 0, 1 ]: for c in [ 0, 1 ]: print( "%3d%3d%3d |%3d%3d%3d" % ( a, b, c, f( a, b, c ), g( a, b, c ), h( a, b, c ) ) ) main()
The output is show below:
a b c | f g h -----------+--------- 0 0 0 | 0 1 1 0 0 1 | 1 1 1 0 1 0 | 0 1 0 0 1 1 | 1 1 0 1 0 0 | 1 1 1 1 0 1 | 1 1 1 1 1 0 | 0 1 1 1 1 1 | 1 0 1