[go: up one dir, main page]

0% found this document useful (0 votes)
11 views14 pages

Report File (Stack)

The document contains multiple Python scripts that implement stack operations for various data types, including students, numbers, hostels, issued books, and more. Each script allows users to push, pop, and display items from the stack, with sample outputs demonstrating the functionality. The scripts utilize lists to manage the stack and include user input for interaction.
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)
11 views14 pages

Report File (Stack)

The document contains multiple Python scripts that implement stack operations for various data types, including students, numbers, hostels, issued books, and more. Each script allows users to push, pop, and display items from the stack, with sample outputs demonstrating the functionality. The scripts utilize lists to manage the stack and include user input for interaction.
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/ 14

Report File

1.
student=[]
def push(student):
name=input("Enter the student name: ")
student.append(name)

def pop(student):
if student==[]:
print("Empty Stack")
else:
print(student.pop())

def display(student):
if student==[]:
print("Empty Stack")
else:
for i in range(len(student)-1,-1,-1):
print(student[i])

while True:
print("Press 1 to PUSH")
print("Press 2 to POP")
print("Press 3 to DISPLAY")
print("Press 4 to EXIT")

choice = int(input("Enter your choice: "))


if choice==1:
push(student)
elif choice==2:
pop(student)
elif choice==3:
display(student)
elif choice==4:
print("Program Exitted")
break
else:
print("Something went wrong")
break
OUTPUT:
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter the student name: Ishita
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter the student name: Ishan
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter the student name: Riya
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 2
Riya
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 3
Ishan
Ishita
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 4
Program Exitted
2.
number=[]
def push(number):
num=int(input("Enter the number you want to PUSH: "))
number.append(num)

def pop(number):
if number==[]:
print("Empty Stack")
else:
print(number.pop())

def display(number):
if number==[]:
print("Empty Stack")
else:
for i in range(len(number)-1,-1,-1):
print(number[i])

while True:
print("Press 1 to PUSH")
print("Press 2 to POP")
print("Press 3 to DISPLAY")
print("Press 4 to EXIT")

choice = int(input("Enter your choice: "))


if choice==1:
push(number)
elif choice==2:
pop(number)
elif choice==3:
display(number)
elif choice==4:
print("Program Exitted")
break
else:
print("Something went wrong")
break
OUTPUT:
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter the number you want to PUSH: 22
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter the number you want to PUSH: 89
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter the number you want to PUSH: 90
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 2
90
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 3
89
22
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 4
Program Exitted
3.
hostel=[]
def push():
HNo=int(input("Enter Hostel Number: "))
TotalStd=int(input("Enter total number of students in hostel: "))
Rooms=int(input("Enter total number of rooms in hostel: "))
hostel.append([HNo,TotalStd,Rooms])

def pop():
for i in range(len(hostel)):
print(hostel.pop())
else:
print("Empty Stack")

def display():
if hostel!=[]:
for j in range(len(hostel)-1,-1,-1):
print(hostel[j])
else:
print("Empty Stack")

while True:
print("Press 1 to PUSH")
print("Press 2 to POP")
print("Press 3 to DISPLAY")
print("Press 4 to EXIT")

choice = int(input("Enter your choice: "))


if choice==1:
push()
elif choice==2:
pop()
elif choice==3:
display()
elif choice==4:
print("Program Exitted")
break
else:
print("Something went wrong")
break
OUTPUT:
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter Hostel Number: 1
Enter total number of students in hostel: 12
Enter total number of rooms in hostel: 8
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter Hostel Number: 2
Enter total number of students in hostel: 15
Enter total number of rooms in hostel: 10
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 1
Enter Hostel Number: 3
Enter total number of students in hostel: 18
Enter total number of rooms in hostel: 12
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 3
[3, 18, 12]
[2, 15, 10]
[1, 12, 8]
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 2
[3, 18, 12]
[2, 15, 10]
[1, 12, 8]
Empty Stack
Press 1 to PUSH
Press 2 to POP
Press 3 to DISPLAY
Press 4 to EXIT
Enter your choice: 4
Program Exitted
4.
Points={'Aryan': 15, 'Kabir': 22, 'Soham': 30, 'Nikhil': 18, 'Zaid': 25}
stk=[]
def push():
for i in Points:
if Points[i]>20:
stk.append(i)

def pop():
if stk==[]:
print("Empty Stack")
else:
for i in range(len(stk)):
print(stk.pop())

OUTPUT:
Zaid
Soham
Kabir
5.
IssuedBooks=[]
def push():
book_id=input("Enter Book ID: ")
title=input("Enter Book Title: ")
IssuedBooks.append((book_id,title))

def display():
print(IssuedBooks[-1])

while True:
print("Print 1 to PUSH")
print("Print 2 to DISPLAY most recently issued book")
print("Print 3 to EXIT")

choice=int(input("Enter your choice: "))


if choice==1:
push()
elif choice==2:
display()
elif choice==3:
print("Program Exitted")
break
else:
print("Something went wrong")
break
OUTPUT:
Print 1 to PUSH
Print 2 to DISPLAY most recently issued book
Print 3 to EXIT
Enter your choice: 1
Enter Book ID: 23
Enter Book Title: Harry Potter
Print 1 to PUSH
Print 2 to DISPLAY most recently issued book
Print 3 to EXIT
Enter your choice: 1
Enter Book ID: 24
Enter Book Title: Matilda
Print 1 to PUSH
Print 2 to DISPLAY most recently issued book
Print 3 to EXIT
Enter your choice: 1
Enter Book ID: 25
Enter Book Title: The Little Prince
Print 1 to PUSH
Print 2 to DISPLAY most recently issued book
Print 3 to EXIT
Enter your choice: 2
('25', 'The Little Prince')
Print 1 to PUSH
Print 2 to DISPLAY most recently issued book
Print 3 to EXIT
Enter your choice: 3
Program Exitted
6.
package_weights={'PKG001': 3.2, 'PKG002': 7.5, 'PKG003': 2.8, 'PKG004':
5.0, 'PKG005': 1.9}
light_stack=[]
def push_light_packages():
for i in package_weights:
if package_weights[i]<5:
light_stack.append(i)

def pop_light_packages():
if light_stack==[]:
print("No more light packages")
else:
for i in range(len(light_stack)):
print(light_stack.pop())
print("No more light packages")

OUTPUT:
PKG005
PKG003
PKG001
No more light packages
7.
records=[['Aarav', 85, 'Drums'], ['Ishita', 92, 'Guitar'], ['Nikhil', 88, 'Guitar'],
['Sana', 75, 'Piano']]
GuitarStack=[]
def push_students():
for i in records:
if i[2]=="Guitar":
GuitarStack.append([i[0],i[1]])

def pop_students():
if GuitarStack==[]:
print("No more guitar players")
else:
for i in range(len(GuitarStack)):
print(GuitarStack.pop())
print("No more guitar players")

OUTPUT:
['Nikhil', 88]
['Ishita', 92]
No more guitar players
8.
d_city = {'Goa': 'Panaji','Punjab': 'Amritsar','Gujarat': 'Ahmedabad','UP':
'Lucknow','Rajasthan': 'Jaipur'}
CITY=[]
def push_city(d_city):
for i in d_city:
if len(i)>4:
CITY.append(d_city[i])

def pop_city():
if CITY==[]:
print("Stack empty")
else:
for i in range(len(CITY)):
print(CITY.pop())
print("Stack Empty")

OUTPUT:
Jaipur
Ahmedabad
Amritsar
Stack Empty
9.
ProductCodes = [2543, 198765, 123, 987654, 45100, 1000000, 4321,
765432]
HighValueProducts=[]
def push_high_value():
for i in ProductCodes:
if i>=100000:
HighValueProducts.append(i)

def pop_high_value():
if HighValueProducts==[]:
print("No high value products left")
else:
for i in range(len(HighValueProducts)):
print(HighValueProducts.pop())
print("No high value products left")

OUTPUT:
765432
1000000
987654
198765
No high value products left
10.
Stu_dict = {5: (87, 68, 89), 10: (57, 54, 61), 12: (71, 67, 90), 14: (66, 81,
80),18: (80, 48, 91)}
Stu_Stk = []
def Push_elements(Stu_Stk,Stu_dict):
for i in Stu_dict:
if Stu_dict[i][2]>=80:
Stu_Stk.append(i)

def Pop_elements(Stu_Stk):
if Stu_Stk==[]:
print("Stack Empty")
else:
for i in range(len(Stu_Stk)):
print(Stu_Stk.pop())
print("Stack Empty")

OUTPUT:
18
14
12
5
Stack Empty

You might also like