Question

In: Computer Science

For this assignment you will implememnt a number of Boolean functions, such as implies, nand, etc....

For this assignment you will implememnt a number of Boolean functions, such as implies, nand, etc.

a) Boolean functions You are provided with a set of undefined functions.You will create the bodies for these 2 parameter functions, such that they return the appropriate Boolean value (True or False). The formal parameters are always p and q (in that order)

Notice the difference between npIMPnq and nqIMPnp: In npIMPnq you return not p implies not q (not first param implies not second param), for example npIMPnq(True,False) returns True.

In nqIMPnp you return not q implies not p (not second param implies not first param), for example nqIMPnp(True,False) returns False.

b) Truth-table inputs The function makettins(n) will create a truth table for all combinations of n Boolean variables. A truth table is an arrayList of 2**n arrayLists of n Booleans. For example makettins(2) returns
[[False, False], [False, True], [True, False], [True, True]]

Notice the recursive nature of makettins(n): It consists of a truth-table(n-1), each row prefixed with False followed by the same truth-table(n-1), each row prefixed with True, with a base case for n==1: [[False], [True]]

Two functions are provided: run(f) and main, so that you can test your code.

'''
PA1: Boolean functions and truth-table inputs
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

For PA1 you will implememnt a number of Boolean functions,
such as implies, nand, etc.

a) Boolean functions
You are provided with a set of undefined functions.You will
create the bodies for these 2 parameter functions, such that
they return the appropriate Boolean value (True or False).
The formal parameters are always p and q (in that order)

Notice the difference between npIMPnq and nqIMPnp:
In npIMPnq you return not p implies not q (not first
param implies not second param), for example
npIMPnq(True,False) returns True.

In nqIMPnp you return not q implies not p (not second
param implies not first param), for example
nqIMPnp(True,False) returns False.

b) Truth-table inputs
The function make_tt_ins(n) will create a truth table for
all combinations of n Boolean variables. A truth table is
an arrayList of 2**n arrayLists of n Booleans. For example
make_tt_ins(2) returns
[[False, False], [False, True], [True, False], [True, True]]

Notice the recursive nature of make_tt_ins(n):
It consists of a truth-table(n-1), each row prefixed with False
followed by the same truth-table(n-1), each row prefixed with True,
with a base case for n==1: [[False], [True]]

Two functions are provided: run(f) and main, so that you can test
your code.

python3 PA1.py tests your Boolean function
python3 PA1.p=y tt tests your function make_tt_ins()

'''


import sys

# p implies q
def implies(p, q):
return False
# not p implies not q
def npIMPnq(p,q):
return False

# not q implies not p
def nqIMPnp(p,q):
return False

# p if and only if q: (p implies q) and (q implies p)
def iff(p, q):
return False

# not ( p and q )
def nand(p, q):
return False

# not p and not q
def npANDnq(p,q):
return False

# not ( p or q)
def nor(p, q):
return False

# not p or not q
def npORnq(p,q):
return False

def make_tt_ins(n):

return [[]]

#provided
def run(f):
print(" True,True : ", f(True,True))
print(" True,False : ", f(True,False))
print(" False,True : ", f(False,True))
print(" False,False: ", f(False,False))
print()
  
#provided
if __name__ == "__main__":
print("program", sys.argv[0])
f1 = sys.argv[1]
print(f1);
if(f1 == "implies"):
run(implies);
if(f1 == "iff"):
run(iff)
if(f1 == "npIMPnq"):
run(npIMPnq)
if(f1 == "nqIMPnp"):
run(nqIMPnp)
if(f1 == "nand"):
run(nand)
if(f1 == "nor"):
run(nor)
if(f1 == "npANDnq"):
run(npANDnq)
if(f1 == "npORnq"):
run(npORnq)
if(f1 == "tt"):
print(make_tt_ins(int(sys.argv[2])))
  

Solutions

Expert Solution

Program Screenshot:

Sample output 1:

Sample output 2:

Code to copy:

import sys

# p implies q

def implies(p, q):

    return not(p) or q

# not p implies not q

def npIMPnq(p,q):

    return p or not(q)

# not q implies not p

def nqIMPnp(p,q):

    return q or not(p)

# p if and only if q: (p implies q) and (q implies p)

def iff(p, q):

    return (not(p) or q) and (not(q) or p)

# not ( p and q )

def nand(p, q):

    return not(p) or not(q)

# not p and not q

def npANDnq(p,q):

    return not(p) and not(q)

# not ( p or q)

def nor(p, q):

    return not(p) and not(q)

# not p or not q

def npORnq(p,q):

    return not(p) or not(q)

# definition of the function make_tt_ins

def make_tt_ins(n):

    #if n value is less than 1,

    #return empty

    if n < 1:

        return [[]]

    #otherwise, call the method recursively

    #with n-1 value

    subtable = make_tt_ins(n-1)

    return [ row + [v] for row in subtable for v in [False,True] ]

#provided

def run(f):

    print(" True,True : ", f(True,True))

    print(" True,False : ", f(True,False))

    print(" False,True : ", f(False,True))

    print(" False,False: ", f(False,False))

    print()

#provided

if __name__ == "__main__":

    print("program", sys.argv[0])

f1 = sys.argv[1]

print(f1);

if(f1 == "implies"):

    run(implies);

if(f1 == "iff"):

    run(iff)

if(f1 == "npIMPnq"):

    run(npIMPnq)

if(f1 == "nqIMPnp"):

    run(nqIMPnp)

if(f1 == "nand"):

    run(nand)

if(f1 == "nor"):

    run(nor)

if(f1 == "npANDnq"):

    run(npANDnq)

if(f1 == "npORnq"):

    run(npORnq)

if(f1 == "tt"):

    print(make_tt_ins(int(sys.argv[2])))


Related Solutions

This assignment will acquaint you with the use of while loops and boolean expressions. You will...
This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points...
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean...
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean algebra (show your work and write the property you are applying). State if they cannot be simplified A. X’Y + XY B. (X + Y)(X + Y’) C. (A’ + B’) (A + B)’ D. ABC + A’B + A’BC’ E. XY + X(WZ + WZ’)
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean...
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean algebra (show your work and write the property you are applying). State if they cannot be simplified. A. A’B + AB B. XY + X(WZ + WZ’) C. X’Y’(X’+Y)(Y’+Y) D. ABC + A’B + A’BC’ E. (A+B)(AC+AC’)+AB+B Draw the circuit logic diagrams for both the original and simplified expressions.
Assignment - Number Guessing with a Class For this assignment you will revisit the number guessing...
Assignment - Number Guessing with a Class For this assignment you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number. For review, a sample run of the program is printed below. In the example the user picks the value 72 for the first game, and then 25 for the second game. The user's input is in bold. Here is how the output might look (exact numbers may vary)...
In this homework assignment, you will be writing three `void` functions. These functions each take a...
In this homework assignment, you will be writing three `void` functions. These functions each take a single integer parameter. The functions accomplish the following tasks. * The first function prints out the sum of the numbers from 1 up to and including the number passed to it as an argument. * The second function prints out the sum of the even numbers from 2 up to and including the number passed to it as an argument. * The third function...
1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number...
1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number of literals: (a) A’C’ + A’BC + B’C (b) (A + B + C)’(ABC)’ (c) ABC’ + AC (d) A’B’D + A’C’D + BD (e) (A’ + B)’(A’ + C’)’(AB’C)’ (f) (AE + A’B’)(C’D’ + CD) + (AC)’ 2. Obtain the truth table of the function F = (AB + C)(B + AC), express the function F in sum-of-minterms and product-of-maxterms forms, and express...
This assignment is For a C++ class , I am having difficulty with getting the boolean...
This assignment is For a C++ class , I am having difficulty with getting the boolean statements the most; getting them set up properly inside the int main() area. You have been asked to define a tip-calculating function named calcTip that could be integrated into a larger piece of software designed to improve guest service at a restaurant. As a result, you must conform to the function specification used throughout the rest of the project's code: double calcTip(double checkAmount, bool...
1) What types of functions, tools, things, etc. would you like to be able to do...
1) What types of functions, tools, things, etc. would you like to be able to do in Excel? Try to be as specific as you can - to the best of your ability! 2) What concerns do you have about learning and/or using Excel? What makes you nervous about it? 3) What uses of Excel have you seen at work or in your personal life? How have you seen it being used? What do you think it is useful for?
) Simplify the following Boolean functions by first finding the essential prime implicants (Please indicate the...
) Simplify the following Boolean functions by first finding the essential prime implicants (Please indicate the essential prime implicants and prime implicants): (a) F(w, x, y, z) = S(0, 1, 2, 4, 5, 6, 8, 10, 13, 15) (b) F(w, x, y, z) = wy’ + xy + y’z + w’xz
discussing advanced functions in Excel (finance functions, If, nested If, Vlookup, Hlookup, etc.). Please research additional...
discussing advanced functions in Excel (finance functions, If, nested If, Vlookup, Hlookup, etc.). Please research additional informative sources in order to increase your understanding of these, or any other, advanced functions. Subsequently, choose an advanced function and write down to educate regarding the meaning and application of it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT