Professional Documents
Culture Documents
Aliza Imran
Aliza Imran
Assignment no 1
DISCRETE STRUCTURES
OUTPUT:
Q5. Program that can take any two variable propositional function from user and prints its truth
table.
def truth table (p, q):
def truth_table(p, q):
print("p\tq\tp and q\tp or q\tp xor q")
print("-"*35)
for i in range(2):
for j in range(2):
conjunction = int(bool(i and j))
disjunction = int(bool(i or j))
exclusive_or = int(bool(i != j))
print(f"{i}\t{j}\t{conjunction}\t{disjunction}\t{exclusive_or}")
truth_table(p, q)
Output:
Q6. Write subroutine that determines whether input logical expression is tautology, contingency
or absurdity (can be T or F).
import itertools
def is_tautology(expression):
variables = set(expression.replace('~', '').replace('&', '').replace('|',
'').replace('(', '').replace(')', ''))
for values in itertools.product([True, False], repeat=len(variables)):
values_dict = dict(zip(variables, values))
if not eval(expression, values_dict):
return False
return True
def is_contingency(expression):
variables = set(expression.replace('~', '').replace('&', '').replace('|',
'').replace('(', '').replace(')', ''))
for values in itertools.product([True, False], repeat=len(variables)):
values_dict = dict(zip(variables, values))
if eval(expression, values_dict):
return True
return False
def is_absurdity(expression):
variables = set(expression.replace('~', '').replace('&', '').replace('|',
'').replace('(', '').replace(')', ''))
for values in itertools.product([True, False], repeat=len(variables)):
values_dict = dict(zip(variables, values))
if eval(expression, values_dict):
return False
return True
if is_tautology(expression):
print('The expression is a tautology.')
elif is_contingency(expression):
print('The expression is a contingency.')
elif is_absurdity(expression):
print('The expression is an absurdity.')
else:
print('Invalid expression.')
OUTPUT:
Q3. Subroutine to fine floor and ceiling of given real number.
import math
OUTPUT:
Q1. Let suppose A and B are two finite sets of integers. Write a subroutine to compute the
following:
A. A U B B. AU B c. A – B
Part A:
def union(A, B):
"""
Returns the union of two sets A and B.
"""
return set(A) | set(B)
A = {7, 2, 3}
B = {8, 9, 5}
print(union(A, B))
OUTPUT:
PART B:
A = {1, 2, 3, 4, 5} # Sample set A
B = {4, 5, 6, 7, 8} # Sample set B
OUTPUT:
Intersection of A and B: {4, 5}
PART C:
def set_difference(A, B):
return A - B
# Example usage
A = {1, 2, 3, 4, 5}
B = {3, 4, 5, 6, 7}
result = set_difference(A, B)
print(result)
OUTPUT:
Q2. Sequence { gn } a recursive one, defined by g(0) = 1, g(1) = -1, g(n) = 3g(n-1) – 2g (n-2).
Write subroutine that print
a. first 15 terms of the sequence
b. first n terms of the sequence. Value of n be taken by user at the run time.