Difference between revisions of "CSC270 GenerateTruthTable.py"

From dftwiki3
Jump to: navigation, search
Line 34: Line 34:
 
The output is show below:
 
The output is show below:
  
<pre><code>
+
<code><pre>
 
   a  b  c  |  f  g   
 
   a  b  c  |  f  g   
 
-----------+--------
 
-----------+--------
Line 46: Line 46:
 
   1  1  1  |  1  0
 
   1  1  1  |  1  0
  
</code></pre>
+
</pre></code>

Revision as of 14:06, 2 February 2009


# truthtable.py
# D. Thiebaut
# how a simple python program can generate the
# truth table of a boolean function
#
# here f is a function of 3 variables
#       _
# f = a.b + c
#     _   _   _
# g = a + b + c
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 main():
    print "  a  b  c  |  f  g  "
    print "-----------+--------"
    for a in [ 0, 1 ]:
        for b in [ 0, 1 ]:
            for c in [ 0, 1 ]:
                print "%3d%3d%3d  |%3d%3d" % \
                      ( a, b, c, f( a, b, c ), g( a, b, c ) )

                
main()

The output is show below:

  a  b  c  |  f  g  
-----------+--------
  0  0  0  |  0  1
  0  0  1  |  1  1
  0  1  0  |  0  1
  0  1  1  |  1  1
  1  0  0  |  1  1
  1  0  1  |  1  1
  1  1  0  |  0  1
  1  1  1  |  1  0