1-34 Program
1-34 Program
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print(f"stack overflow")
else:
stack.append(value)
print(f"book added {value} onto stack")
def pop():
if len(stack)==0:
print(f"stack underflow")
else:
value=stack.pop()
print(f"removed book {value} onto stack")
def display():
if len(stack)==0:
print("stack is empty")
else:
print("stack of book are :")
for elements in reversed(stack):
print(elements)
def main():
while True:
print("\nstack operation")
print("1.push\n2.pop\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the book name:")
push(value)
elif choice==2:
pop()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
2)
arr=["UK","USA","UAE","INDIA"]
country=input("enter the name of the country to search:").strip('"')
if country in arr:
print(f"country {country} is found in the list")
else:
print(f"country {country} is not found")
3)
class Node:
def __init__(self,data):
self.data=data
self.next=None
class linkedlist:
def __init__(self):
self.head=None
def insert(self,data):
if not self.head:
self.head=Node(data)
else:
last=self.head
while last.next:
last=last.next
last.next=Node(data)
def display(self):
current=self.head
if not current:
print("the list is empty")
return
element=[]
while current:
print(current.data)
current= current.next
ll=linkedlist()
for i in ["UK","UAE","USA","INDIA"]:
ll.insert(i)
ll.display()
4)
class Node:
def __init__(self,data):
self.data=data
self.next=None
self.prev=None
class doublelinkedlist:
def __init__(self):
self.head=None
def append(self,data):
if not self.head:
self.head=Node(data)
else:
last=self.head
while last.next:
last=last.next
last.next=Node(data)
Node(data).prev=last
def display(self):
current=self.head
if not current:
print("list is empty")
return
print("student enrolled course:")
while current:
print(current.data)
current=current.next
dll=doublelinkedlist()
for i in ["FAZIL","ABRAR","SRI","ALI"]:
dll.append(i)
dll.display()
5)
max_size=5
queue=[]
def enqueue(value):
if len(queue)>=max_size:
print("library has book")
else:
queue.append(value)
print(f"enqueue books {value} in library")
def dequeue():
if not queue:
print("library has no books")
else:
print(f"dnqueue books {queue.pop(0)} in library")
def display():
if not queue:
print("library has no books")
else:
print("library books are:",*queue)
def main():
while True:
print("1.enqueue\n2.dequeue\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the name of the book:")
enqueue(value)
elif choice==2:
dequeue()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
6)
def sort_string(strings):
return sorted(strings)
if __name__=="__main__":
string=["bala","ali","sri","kalith","fazil"]
string=sort_string(string)
print("sorted list are:")
for i in string:
print(i)
7)
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("food is not available")
else:
stack.append(value)
print(f"food {value} in the list")
def display():
if len(stack)==0:
print("list is empty")
else:
print("food items are:")
for items in reversed(stack):
print(items)
def main():
while True:
print("1.push\n2.display\n3.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the food name:")
push(value)
elif choice==2:
display()
else:
print("invalid choice!")
break
main()
8)
def x(arr):
x= arr[0]
for i in arr:
if i < x:
x=i
return x
arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
print("The minimum element in the array is:", x(arr))
9)
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("browsing history")
else:
stack.append(value)
print(f"visited {value} on stack ")
def pop():
if len(stack)==0:
print("browsing history")
else:
print(f"going back from{stack.pop(0)} on stack ")
def display():
if len(stack)==0:
print("browsing history available")
else:
print(f"browsing history on stack ")
for site in reversed(stack):
print(stack)
def main():
while True:
print("1.visited(push) \n2.going back(pop)\n3.display
history(display)\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the URL :")
push(value)
elif choice==2:
pop()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
10)
queue=[]
def enqueue(value):
if len(queue)<=max_size:
print("queue overflow")
else:
queue.append(value)
print(f"charater {value} is enqueue")
def dequeue():
if not queue:
print("queue underflow")
else:
print(f"charater {queue.pop(0)} is dequeue")
def display():
if not queue:
print("queue is empty")
else:
print(f"charater are:",*queue)
def main():
while True:
print("1.enqueue\n2.dequeue\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the charater to enqueue:")
enqueue(value)
elif choice==2:
dequeue()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
11)
class Node:
def __init__(self,key):
self.left=None
self.right=None
self.val=key
def post_order_traversal(root):
if root:
post_order_traversal(root.left)
post_order_traversal(root.right)
print(root.val,end='')
if __name__=="__main__":
root = Node("CEO,")
root.left = Node("CTO,")
root.right = Node("CFO,")
root.left.left = Node("Dev1,")
root.left.right = Node("Dev2,")
root.right.left = Node("Account1,")
root.right.right = Node("Account2,")
print("\nPost-order traversal:")
post_order_traversal(root)
12(prime number) is similar as 28(odd number)
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("stack overflow")
else:
stack.append(value)
print(f"pushed {value} onto the stack")
def pop():
if len(stack)==0:
print("stack underflow")
else:
print(f"popped {stack.pop(0)} onto the stack")
def display():
if len(stack)==0:
print("stack is empty")
else:
print("stack elements are:")
for i in reversed(stack):
print(i)
def main():
prime_numbers = [2, 3, 5, 7, 11] # First five prime numbers
print("Pushing first five prime numbers onto the stack:")
for i in prime_numbers:
push(i)
display()
print("\nPopping elements from the stack:")
for _ in range(3): # Pop three elements
pop()
display()
if __name__ == "__main__":
main()
13 is same as 2
14) is same as 31
from queue import Queue
course=Queue()
course.put("ai")
course.put("cse")
course.put("python")
x=course.qsize()
print(f"the size of courses {x}")
while not course.empty():
d=course.get()
print(d)
15) and 33 is same
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("ticket booked already")
else:
stack.append(value)
print(f"train ticket {value} booked")
def pop():
if len(stack)==0:
print("ticket booked cancelled")
else:
print(f"train ticket {stack.pop(0)} are cancelled")
def display():
if len(stack)==0:
print("train ticket empty")
else:
print(f"train ticket are:")
for i in reversed(stack):
print(i)
def main():
while True:
print("1.push\n2.pop\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the train ticket:")
push(value)
elif choice==2:
pop()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
16)
class Node:
def __init__(self,key):
self.left=None
self.right=None
self.val=key
def pre_order_traversal(root):
if root:
print(root.val,end='')
pre_order_traversal(root.left)
pre_order_traversal(root.right)
if __name__=="__main__":
root=Node("CEO,")
root.left=Node("CTO,")
root.right=Node("CFO,")
root.left.left=Node("DEV1,")
root.left.right=Node("DEV2,")
root.right.left=Node("ACCOUNT1,")
root.right.right=Node("ACCOUNT2,")
print("\npre-order traversal")
pre_order_traversal(root)
17(student) is same as 34(employee)
class Node:
def __init__(self, name):
self.name = name
self.right = None
self.left = None
class BinarySearch:
def __init__(self):
self.root = None
def insert(self, root, name):
if root is None:
return Node(name)
if name < root.name:
root.left = self.insert(root.left, name)
else:
root.right = self.insert(root.right, name)
return root
def search(self, root, name):
if root is None or root.name == name:
return root
if name > root.name:
return self.search(root.right, name)
return self.search(root.left, name)
def insert_name(self, name):
if self.root is None:
self.root = Node(name)
else:
self.insert(self.root, name)
def find_name(self, name):
result = self.search(self.root, name)
if result:
return f"'{name}' found."
else:
return f"'{name}' not found."
bst = BinarySearch()
students = ["a", "b", "c"]
for student in students:
bst.insert_name(student)
search_name = "a"
print(bst.find_name(search_name))
18)
def x(arr):
x=arr[0]
for i in arr:
if i<x:
x=i
return x
arr=[11,2,34,563,646]
print("the maximum number is:",x(arr))
19)
arr=[1,2,3,4,5]
x=2
found=False
for i in range(2):
if arr[i]==x:
print(f"element {x} is searched and found")
found=True
if not found:
print(f"element is not found")
20)
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("country overflow")
else:
stack.append(value)
print(f"country {value} onto stack")
def pop():
if len(stack)==0:
print("country underflow")
else:
print(f"country {stack.pop(0)} onto stack")
def display():
if len(stack)==0:
print("country is empty")
else:
print(f"country in world map:")
for i in reversed(stack):
print(i)
def main():
while True:
print("1.push\n2.pop\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the country to push:")
push(value)
elif choice==2:
pop()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
21)
class Node:
def __init__(self,data):
self.data=data
self.next=None
class linkedlist:
def __init__(self):
self.head=None
def insert(self,data):
if not self.head:
self.head=Node(data)
else:
last=self.head
while last.next:
last=last.next
last.next=Node(data)
def display(self):
current=self.head
if not current:
print("list is empty")
return
element=[]
while current:
print(current.data)
current=current.next
ll=linkedlist()
for i in ["GRAPES","MANGO","BANANA","APPLE"]:
ll.insert(i)
ll.display()
22)
class Node:
def __init__(self,data):
self.data=data
self.next=None
self.prev=None
class doublelinkedlist:
def __init__(self):
self.head=None
def insert(self,data):
if not self.head:
self.head=Node(data)
else:
last=self.head
while last.next:
last=last.next
last.next=Node(data)
Node(data).prev=last
def display(self):
current=self.head
if not current:
print("list is empty")
return
print(" candidates applied for a job:")
element=[]
while current:
print(current.data)
current=current.next
dll=doublelinkedlist()
for i in ["MANAGER","HR","APPS DEVELOPER","SERVER MANAGER "]:
dll.insert(i)
dll.display()
23)
24)
def sort_array(arr):
arr.sort()
return arr
numbers = [34, 23, 12, 56, 9, 11, 78]
print("Original array:", numbers)
sorted_numbers = sort_array(numbers)
print("Sorted array:", sorted_numbers)
25)
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("stack overflow")
else:
stack.append(value)
print(f"sports {value} onto stack")
def pop():
if len(stack)==0:
print("stack underflow")
else:
print(f"removed {stack.pop(0)} in the sports")
def display():
if len(stack)==0:
print("tack is empty")
else:
print(f"sports meet:")
for i in reversed(stack):
print(i)
def main():
while True:
print("1.push\n2.pop\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the sports to push:")
push(value)
elif choice==2:
pop()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
26)
stack=[]
max_size=5
def push(value):
if len(stack)==max_size:
print("cart overflow")
else:
stack.append(value)
print(f"adding list {value} in the cart")
def pop():
if len(stack)==0:
print("cart underflow")
else:
print(f"removed list {stack.pop(0)} in the cart")
def display():
if len(stack)==0:
print("cart list is empty")
else:
print("cart list are:")
for i in reversed(stack):
print(i)
def main():
while True:
print("1.push\n2.pop\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the cart lists to push:")
push(value)
elif choice==2:
pop()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
27)
max_size=5
queue=[]
def enqueue(value):
if len(queue)>=max_size:
print("string overflow")
else:
queue.append(value)
print(f"enqueue colors {value} in the queue")
def dequeue():
if not queue:
print("string underflow")
else:
print(f"dequeue colors {queue.pop(0)} in the queue")
def display():
if not queue:
print("queue is empty")
else:
print(f"\ncolors are:\n",*queue)
def main():
while True:
print("1.enqueue\n2.dequeue\n3.display\n4.exit")
choice=int(input("enter your choice:"))
if choice==1:
value=input("enter the color to enqueue:")
enqueue(value)
elif choice==2:
dequeue()
elif choice==3:
display()
else:
print("invalid choice!")
break
main()
29)
arr1=[[0]*3 for _ in range(3)]
arr2=[[0]*3 for _ in range(3)]
result_sum=[[0]*3 for _ in range(3)]
print("\nenter the arr1")
for i in range(3):
for j in range(3):
arr1[i][j]=int(input(f"enter the value for arr1[{i}][{j}]="))
print("\nenter the arr2")
for i in range(3):
for j in range(3):
arr2[i][j]=int(input(f"enter the value for arr2[{i}][{j}]="))
for i in range(3):
for j in range(3):
result_sum[i][j]=0
for k in range(3):
result_sum[i][j]+=arr1[i][k]*arr2[k][j]
print("\n the result of matrices multiply")
for i in range(3):
for j in range(3):
print(f"result_sum[{i}][{j}]={result_sum[i][j]}")
30)
queue = []
max_size = 10
def enqueue(value):
if len(queue) >= max_size:
print("Queue overflow: Cannot add more students")
else:
queue.append(value)
print(f"Student '{value}' has been added to the seminar.")
def dequeue():
if not queue:
print("Queue underflow: No students to remove")
else:
print(f"Student '{queue.pop(0)}' has been removed from the seminar.")
def display():
if not queue:
print("No students are attending the seminar.")
else:
print("List of students attending the seminar:", *queue)
def number_students():
print(f"Number of students attending the seminar: {len(queue)}")
def main():
while True:
print("\n1. Add a student to the seminar")
print("2. Remove a student from the seminar")
print("3. Display all students attending the seminar")
print("4. Get the number of students attending the seminar")
print("5. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
student = input("Enter the name of the student to add: ")
enqueue(student)
elif choice == 2:
dequeue()
elif choice == 3:
display()
elif choice == 4:
number_students()
else:
print("Invalid choice. Please try again.")
break
main()
32)
arr1=[[0]*3 for _ in range(3)]
arr2=[[0]*3 for _ in range(3)]
sum_array=[[0]*3 for _ in range(3)]
print("\nenter the value for arr1")
for i in range(3):
for j in range(3):
arr1[i][j]=int(input(f"enter the value for arr1[{i}][{j}]="))
print("\nenter the value for arr2")
for i in range(3):
for j in range(3):
arr2[i][j]=int(input(f"enter the value for arr2[{i}][{j}]="))
for i in range(3):
for j in range(3):
sum_array[i][j]=arr1[i][j]+arr2[i][j]
print("\nsum of matrix")
for i in range(3):
for j in range(3):
print(f"sum_array[{i}][{j}]={sum_array[i][j]}")