0% found this document useful (0 votes)
23 views4 pages

New Text Document

The document defines functions for sorting a list of student objects by name, age, department or grade. It creates a list of student objects, sorts the list based on user input criteria and prints the sorted list.

Uploaded by

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

New Text Document

The document defines functions for sorting a list of student objects by name, age, department or grade. It creates a list of student objects, sorts the list based on user input criteria and prints the sorted list.

Uploaded by

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

def __repr__(self):

return f"Student('{self.name}', {self.age}, '{self.department}',


'{self.grade}')"

def sort_students(students, criteria):


if criteria == 1:
students.sort(key=lambda student: student.name)
elif criteria == 2:
students.sort(key=lambda student: student.age)
elif criteria == 3:
students.sort(key=lambda student: student.department)
elif criteria == 4:
students.sort(key=lambda student: student.grade)
else:
print("Invalid criteria.")

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

print("Enter students details:")


print_students(students)

criteria = int(input("Choose criteria to sort: 1. Name, 2. Age, 3. Department, 4.


Grade: "))
sort_students(students, criteria)

if criteria in [1, 2, 3, 4]:


print(f"\nSorting {['Name', 'Age', 'Department', 'Grade'][criteria - 1]}
wise:")
print_students(students)

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

while queue and i < len(elements):


current_node = queue.pop(0)
if elements[i] is not None:
current_node.left = Node(elements[i])
queue.append(current_node.left)
i += 1

if i < len(elements) and elements[i] is not None:


current_node.right = Node(elements[i])
queue.append(current_node.right)
i += 1

return root

def find_lca(root, node1, node2):


if root is None:
return None

if root.value == node1 or root.value == node2:


return root

left_lca = find_lca(root.left, node1, node2)


right_lca = find_lca(root.right, node1, node2)

if left_lca and right_lca:


return root

return left_lca if left_lca else right_lca

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 multiply_matrices(matrix1, matrix2):


rows1 = len(matrix1)
cols1 = len(matrix1[0])
cols2 = len(matrix2[0])
result = [[0] * cols2 for _ in range(rows1)]
for I in range(rows1):
for j in range(cols2):
for k in range(cols1):
result[i][j] += matrix1[i][k] * matrix2[k][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

def find_max_multiplication(matrix1, matrix2):


rows1 = len(matrix1)
cols1 = len(matrix1[0])
cols2 = len(matrix2[0])
max_value = float('- inf')
for I in range(rows1):
for j in range(cols2):
curr_value = 0
for k in range(cols1):
curr_value += matrix1[i][k] * matrix2[k][j]
max_value = max(max_value, curr_value)
bring max_value back

matrix1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


matrix2 = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

expansion = add_matrices(matrix1, matrix2)


deduction = subtract_matrices(matrix1, matrix2)
increase = multiply_matrices(matrix1, matrix2)
render = transpose_matrix(matrix1)
max_multiplication = find_max_multiplication(matrix1, matrix2)

print("Matrix Expansion:", expansion)


print("Matrix Deduction:", deduction)
print("Matrix Increase:", duplication)
print("Maximum worth of increase is:", max_multiplication)
print("Matrix Render 1:", translate)

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)

if abs(left_height - right_height) <= 1 and is_balanced(root.left) and


is_balanced(root.right):
return True

return False
def get_height(node):
if node is None:
return 0

left_height = get_height(node.left)
right_height = get_height(node.right)

return max(left_height, right_height) + 1

def build_tree(nodes):
node_dict = {}
root = None

for value in nodes:


node = Node(value)
node_dict[value] = node

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

node_values = input("Enter Nodes: ").split()


nodes = [int(value) for value in node_values]

root = build_tree(nodes)

if is_balanced(root):
print("Tree is balanced.")
else:
print("Tree is not balanced.")

print("Root is", root.value)

OUTPUT:
Enter Nodes: 1 2 3 4 5 8
Tree is balanced.
Root is 4

You might also like