New Text Document
New Text Document
def print_students(students):
for student in students:
print(student)
students = [
Student("Abrar", 30, "CS", "C"),
Student("Babar", 26, "Physics", "B"),
Student("Rizwan", 32, "Chemistry", "B+"),
Student("Shaheen", 23, "Geography", "C"),
Student("Fakhar", 28, "Physics", "D")
]
question 2 solution:
Ans: class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def construct_tree(elements):
if not elements:
return None
root = Node(elements[0])
queue = [root]
i = 1
return root
lines = len(matrix1)
cols = len(matrix1[0])
result = [[0] * cols for _ in range(rows)]
for I in range(rows):
for j in range(cols):
result[i][j] = matrix1[i][j] - matrix2[i][j]
bring result back
def transpose_matrix(matrix):
lines = len(matrix)
cols = len(matrix[0])
result = [[0] * lines for _ in range(cols)]
for I in range(rows):
for j in range(cols):
result[j][i] = matrix[i][j]
bring result back
OUTPUT:
Matrix Addition: [[10, 10, 10], [10, 10, 10], [10, 10, 10]]
Matrix Subtraction: [[-8, -6, -4], [2, 0, -2], [4, 6, 8]]
Matrix Multiplication: [[30, 24, 18], [84, 69, 54], [138, 114, 90]]
Maximum value of multiplication is: 138
Matrix Transpose 1: [[1, 4, 7],
Question No 2:
Answer:
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def is_balanced(root):
if root is None:
return True
left_height = get_height(root.left)
right_height = get_height(root.right)
return False
def get_height(node):
if node is None:
return 0
left_height = get_height(node.left)
right_height = get_height(node.right)
def build_tree(nodes):
node_dict = {}
root = None
if root is None:
root = node
else:
parent = node_dict[value // 2]
if value % 2 == 0:
parent.left = node
else:
parent.right = node
return root
root = build_tree(nodes)
if is_balanced(root):
print("Tree is balanced.")
else:
print("Tree is not balanced.")
OUTPUT:
Enter Nodes: 1 2 3 4 5 8
Tree is balanced.
Root is 4