Difference between revisions of "CSC270 Homework 1 2011 Solution"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- This is the solution page for the CSC270 Homework #1. =Problem #1= f = a' + bc g = a' . (b' + c' ) h = 1 k = a' + b + c' =Problem...")
 
Line 27: Line 27:
 
  h = Π( ) = 1
 
  h = Π( ) = 1
 
  k = Π( 5 )
 
  k = Π( 5 )
 +
 +
=Program=
 +
<code><pre>
 +
# ----------------------------------------------------------------------------
 +
# File: hw1.py
 +
# Author: Tiffany Q. Liu
 +
# Acct: 270b-ae
 +
# Date: February 4, 2010
 +
# Desc: Modification of code take from D. Thiebaut used to verify that the
 +
#      simplified boolean expressions are equivalent to their non-simplified
 +
#      counterparts. Functions are defined such that fcnName1 = original,
 +
#      non-simplified boolean expression and fcnName2 = simplified boolean
 +
#      expression.
 +
#
 +
# Output:
 +
#  a  b  c  |  f1  f2  |  g1  g2  |  h1  h2  |  k1  k2 
 +
#-----------+----------+----------+----------+----------
 +
#  0  0  0  |  1  1  |  1  1  |  1  1  |  1  1
 +
#  0  0  1  |  1  1  |  1  1  |  1  1  |  1  1
 +
#  0  1  0  |  1  1  |  1  1  |  1  1  |  1  1
 +
#  0  1  1  |  1  1  |  0  0  |  1  1  |  1  1
 +
#  1  0  0  |  0  0  |  0  0  |  1  1  |  1  1
 +
#  1  0  1  |  0  0  |  0  0  |  1  1  |  0  0
 +
#  1  1  0  |  0  0  |  0  0  |  1  1  |  1  1
 +
#  1  1  1  |  1  1  |  0  0  |  1  1  |  1  1
 +
#
 +
# ----------------------------------------------------------------------------
 +
 +
# -------------------------------------------------------------
 +
# f1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c' + a'.b.c + a.b.c
 +
# -------------------------------------------------------------
 +
def f1(a,b,c):
 +
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
 +
          ((not a) & b & (not c)) | ((not a) & b & c) | (a & b & c)
 +
 +
# -------------------------------------------------------------
 +
# f2(a,b,c) = a' + b.c
 +
# -------------------------------------------------------------
 +
def f2(a,b,c):
 +
    return (not a) | (b & c)
 +
 +
# -------------------------------------------------------------
 +
# g1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c'
 +
# -------------------------------------------------------------
 +
def g1(a,b,c):
 +
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
 +
          ((not a) & b & (not c))
 +
 +
# -------------------------------------------------------------
 +
# g2(a,b,c) = a'.b' + a'.c'
 +
# -------------------------------------------------------------
 +
def g2(a,b,c):
 +
    return ((not a) & (not b)) | ((not a) & (not c))
 +
 +
# -------------------------------------------------------------
 +
# h1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c' + a'.b.c + a.b'.c'
 +
#            + a.b'.c + a.b.c' + a.b.c
 +
# -------------------------------------------------------------
 +
def h1(a,b,c):
 +
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
 +
          ((not a) & b & (not c)) | ((not a) & b & c) | \
 +
          (a & (not b) & (not c)) | (a & (not b) & c) | (a & b & (not c)) | \
 +
          (a & b & c)
 +
 +
# -------------------------------------------------------------
 +
# h2(a,b,c) = 1
 +
# -------------------------------------------------------------
 +
def h2(a,b,c):
 +
    return 1
 +
 +
# -------------------------------------------------------------
 +
# k1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c' + a'.b.c + a.b'.c'
 +
#            + a.b.c' + a.b.c
 +
# -------------------------------------------------------------
 +
def k1(a,b,c):
 +
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
 +
          ((not a) & b & (not c)) | ((not a) & b & c) | \
 +
          (a & (not b) & (not c)) | (a & b & (not c)) | (a & b & c)
 +
 +
# -------------------------------------------------------------
 +
# k2(a,b,c) = a' + a.b + a.c'
 +
# -------------------------------------------------------------
 +
def k2(a,b,c):
 +
    return (not a) | (a & b) | (a & (not c))
 +
 +
# -------------------------------------------------------------
 +
# Print out truth table for f1, f2, g1, g2, h1, h2, k1, and k2.
 +
# -------------------------------------------------------------
 +
def main():
 +
    print "  a  b  c  |  f1  f2  |  g1  g2  |  h1  h2  |  k1  k2  "
 +
    print "-----------+----------+----------+----------+----------"
 +
    for a in [0,1]:
 +
        for b in [0,1]:
 +
            for c in [0,1]:
 +
                print "%3d%3d%3d  |%3d%4d  |%3d%4d  |%3d%4d  |%3d%4d" % \
 +
                      (a, b, c, f1(a,b,c), f2(a,b,c), g1(a,b,c), g2(a,b,c),
 +
                      h1(a,b,c), h2(a,b,c), k1(a,b,c), k2(a,b,c))
 +
 +
main()
 +
 +
 +
</pre></code>
  
 
[[Category:CSC270]][[Category:Homework]]
 
[[Category:CSC270]][[Category:Homework]]

Revision as of 12:36, 8 February 2011

--D. Thiebaut 12:10, 8 February 2011 (EST)


This is the solution page for the CSC270 Homework #1.

Problem #1

f = a' + bc
g = a' . (b' + c' )
h = 1
k = a' + b + c'

Problem #2

f = x'.y + x.y' + x.z
  = x ^ y  + x.z     
where ^ represents xor
f = Σ( 2, 3, 4, 5, 7 )
f = Π ( 0, 1, 6 )

Problem #3

f = Π( 4, 5, 6 )
g = Π( 3, 4, 5, 6, 7 )
h = Π( ) = 1
k = Π( 5 )

Program

# ----------------------------------------------------------------------------
# File: hw1.py
# Author: Tiffany Q. Liu
# Acct: 270b-ae
# Date: February 4, 2010
# Desc: Modification of code take from D. Thiebaut used to verify that the 
#       simplified boolean expressions are equivalent to their non-simplified
#       counterparts. Functions are defined such that fcnName1 = original,
#       non-simplified boolean expression and fcnName2 = simplified boolean
#       expression.
#
# Output:
#  a  b  c  |  f1  f2  |  g1  g2  |  h1  h2  |  k1  k2  
#-----------+----------+----------+----------+----------
#  0  0  0  |  1   1   |  1   1   |  1   1   |  1   1
#  0  0  1  |  1   1   |  1   1   |  1   1   |  1   1
#  0  1  0  |  1   1   |  1   1   |  1   1   |  1   1
#  0  1  1  |  1   1   |  0   0   |  1   1   |  1   1
#  1  0  0  |  0   0   |  0   0   |  1   1   |  1   1
#  1  0  1  |  0   0   |  0   0   |  1   1   |  0   0
#  1  1  0  |  0   0   |  0   0   |  1   1   |  1   1
#  1  1  1  |  1   1   |  0   0   |  1   1   |  1   1
#
# ----------------------------------------------------------------------------

# -------------------------------------------------------------
# f1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c' + a'.b.c + a.b.c
# -------------------------------------------------------------
def f1(a,b,c):
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
           ((not a) & b & (not c)) | ((not a) & b & c) | (a & b & c)

# -------------------------------------------------------------
# f2(a,b,c) = a' + b.c
# -------------------------------------------------------------
def f2(a,b,c):
    return (not a) | (b & c)

# -------------------------------------------------------------
# g1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c'
# -------------------------------------------------------------
def g1(a,b,c):
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
           ((not a) & b & (not c))

# -------------------------------------------------------------
# g2(a,b,c) = a'.b' + a'.c'
# -------------------------------------------------------------
def g2(a,b,c):
    return ((not a) & (not b)) | ((not a) & (not c))

# -------------------------------------------------------------
# h1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c' + a'.b.c + a.b'.c' 
#            + a.b'.c + a.b.c' + a.b.c
# -------------------------------------------------------------
def h1(a,b,c):
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
           ((not a) & b & (not c)) | ((not a) & b & c) | \
           (a & (not b) & (not c)) | (a & (not b) & c) | (a & b & (not c)) | \
           (a & b & c)

# -------------------------------------------------------------
# h2(a,b,c) = 1
# -------------------------------------------------------------
def h2(a,b,c):
    return 1

# -------------------------------------------------------------
# k1(a,b,c) = a'.b'.c' + a'.b'.c + a'.b.c' + a'.b.c + a.b'.c' 
#            + a.b.c' + a.b.c
# -------------------------------------------------------------
def k1(a,b,c):
    return ((not a) & (not b) & (not c)) | ((not a) & (not b) & c) | \
           ((not a) & b & (not c)) | ((not a) & b & c) | \
           (a & (not b) & (not c)) | (a & b & (not c)) | (a & b & c)

# -------------------------------------------------------------
# k2(a,b,c) = a' + a.b + a.c'
# -------------------------------------------------------------
def k2(a,b,c):
    return (not a) | (a & b) | (a & (not c))

# -------------------------------------------------------------
# Print out truth table for f1, f2, g1, g2, h1, h2, k1, and k2.
# -------------------------------------------------------------
def main():
    print "  a  b  c  |  f1  f2  |  g1  g2  |  h1  h2  |  k1  k2  "
    print "-----------+----------+----------+----------+----------"
    for a in [0,1]:
        for b in [0,1]:
            for c in [0,1]:
                print "%3d%3d%3d  |%3d%4d   |%3d%4d   |%3d%4d   |%3d%4d" % \
                      (a, b, c, f1(a,b,c), f2(a,b,c), g1(a,b,c), g2(a,b,c),
                       h1(a,b,c), h2(a,b,c), k1(a,b,c), k2(a,b,c))

main()