0% found this document useful (0 votes)
34 views6 pages

Aliza Imran

Here are the subroutines to print the terms of the recursive sequence defined by g(0) = 1, g(1) = -1, g(n) = 3g(n-1) - 2g(n-2): Part a: def print_sequence(n): g = [1, -1] for i in range(2,n+1): g.append(3*g[i-1] - 2*g[i-2]) print(g) print_sequence(15) Part b: def print_n_terms(n): g = [1, -1] for i in

Uploaded by

Alixa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Aliza Imran

Here are the subroutines to print the terms of the recursive sequence defined by g(0) = 1, g(1) = -1, g(n) = 3g(n-1) - 2g(n-2): Part a: def print_sequence(n): g = [1, -1] for i in range(2,n+1): g.append(3*g[i-1] - 2*g[i-2]) print(g) print_sequence(15) Part b: def print_n_terms(n): g = [1, -1] for i in

Uploaded by

Alixa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

University of Engineering and Technology, Taxila

Assignment no 1

DISCRETE STRUCTURES

Submitted To: Dr Arta Iftikhar


Submitted By: Aliza Imran
Reg No: 22-SE-25

DEPARTMENT OF SOFTWARE ENGINEERING


Q4. Program to print truth table for p → q.
print ("Truth Table for p implies q")
print ("p\tq\tp implies q")
print ("------------------------")
# Define the possible values for p and q
p_values = [True, True, False, False]
q_values = [True, False, True, False]
# Loop through all possible combinations of p and q
for i in range(4):
p = p_values[i]
q = q_values[i]
if p:
p_str = "T"
else:
p_str = "F"
if q:
q_str = "T"
else:
q_str = "F"
# Calculate p implies q and print the result
if not p or q:
result_str = "T"
else:
result_str = "F"
print(f"{p_str}\t{q_str}\t{result_str}")

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}")

p = input("Enter first variable (True/False): ").lower() == "true"


q = input("Enter second variable (True/False): ").lower() == "true"

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

expression = input('Enter a logical expression: ')

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

# get the user input


num = float(input("Enter a real number: "))

# find the floor and ceiling


floor = math.floor(num)
ceiling = math.ceil(num)

# print the results


print("The floor of", num, "is", floor)
print("The ceiling of", num, "is", ceiling)

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

# Using the built-in intersection method to compute the intersection of A and B


intersection = A.intersection(B)
print("Intersection of A and B:", intersection)

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.

You might also like