Showing posts with label Stack Programs. Show all posts
Showing posts with label Stack Programs. Show all posts

Data Structure Test 4

 Data Structure Test 4

Q1. A linear stack called "List" contain the following information:
            a. Roll Number of student
            b. Name of student
Write add(List) and pop(List) methods in python to add and remove from the stack.

Ans. 

List=[]
def add(List):
rno=int(input("Enter roll number"))
name=input("Enter name")
item=[rno,name]
List.append(item)
def pop(List):
if len(List)>0:
List.pop()
else:
print("Stack is empty")
#Call add and pop function to verify the code
add(List)
add(List)
pop(List)
print(List)

Q2. Write push(edetail) and pop(edetail) in python to add and remove the employee detail in a stack called "edetail".

"edetail" stack store the following details:

       a. Name of employee

       b. Salary of employee

Ans.

edetail = []
def push(edetail):
name = input("Enter name")
sal = int(input("Enter Salary"))
item = [name, sal]
edetail.append(item)
def pop(edetail):
if len(edetail) > 0:
edetail.pop()
else:
print("Stack is empty")

#Call push and pop function to verify the code
push(edetail)
push(edetail)
pop(edetail)
print(edetail)

Q3. Write addscore(game) and delscore(game) methods in python to add and remove score from a list "game", considering these methods to act as push and pop operations of data structure stack.

Ans.

game = []
def addscore(game):
sc=int(input("Enter Score"))
game.append(sc)
def pop(ed):#in place of 'ed' we can use 'game'also
if len(ed) > 0:
ed.pop()
else:
print("Stack is empty")
#Call functions to verify the code
addscore(game)
addscore(game)
pop(game)
print(game)

OR (When we don't want to accept value from
the user)

game = []
def addscore(sc):
game.append(sc)
def pop():
if len(game) > 0:
game.pop()
else:
print("Stack is empty")
#Call functions to verify the code
addscore(23)
addscore(54)
pop()
print(game)

Q4. Write addsal(sal) and removesal(sal) functions in python to add and remove salary from a list of salary in a list "sal", considering these methods to act as push and pop operations of data structure stack.

Ans.

sal = []
def addsal(sc):
sal.append(sc)
def pop():
if len(sal) > 0:
sal.pop()
else:
print("Stack is empty")
#Call functions to verify the code
addsal(23)
addsal(54)
pop()
print(sal)

Q5. Write addfee() function in python to add the following detail in list named "fee".
        feeamount
        feemonth
Considering this as push function of stack
Ans.
fee=[]
def addfee(fee):
fa=int(input("Enter the fee amount"))
fm = input("Enter the fee month")
item = [fa,fm]
fee.append(item)
addfee(fee)

Data Structure Test 3


Data Structure Test 3

Q1. Write a function Push() which takes number as 
    argument and add in a  stack "MyValue"

Q2. Write a function Push that takes "name" as 
    argument and add in a stack named "MyStack"
 
Q3. Write a function Push() which takes "name" as
    argument and add in a stack named "MyStack".
    After calling push() three times, a message
    should be displayed "Stack is Full"

Q4. Write a function pop() which remove name
    from stack named "MyStack".
 
Q5. Write push(rollno) and pop() method in python:
    push(rollno) --add roll number in Stack
    pop() --- remove roll number from Stack.
 
 
Q6. Write add(bookname) and delete() method in  
    python to add bookname and remove bookname 
    considering them to act as push() and pop() 
    operations in stack.
 
Q7. Write addclient(clientname) and remove()
    methods in python to add new client and delete
    existing client from a list "clientdetail",
    considering them to act as push and pop
    operations of the stack.

 
SOLUTIONS

Q1. Write a function Push() which takes number as argument and add in a  stack "MyValue"
Ans. 
MyValue=[]
def Push(value):
  MyValue.append(value)

Q2. Write a function Push that takes "name" as argument and add in a stack named
"MyStack"
Mynames=[]
def Push(Value):
    Mynames.append(Value)
Push("Amit")
 
Q3. Write a function Push() which takes "name" as
argument and add in a stack named "MyStack".
After calling push() three times, a message
should be displayed "Stack is Full"
Ans.
MyStack=[]
StackSize=3
def Push(Value):
   if len(MyStack) < StackSize:
       MyStack.append(Value)
   else:
       print("Stack is full!")
 
Q4. Write a function pop() which remove name
from stack named "MyStack".
Ans.
def Pop(MyStack):
 if len(MyStack) > 0:
  MyStack.pop()
 else:
  print("Stack is empty.")
 
Q5. Write push(rollno) and pop() method in python:
    push(rollno) --add roll number in Stack
    pop() --- remove roll number from Stack.
 
Ans. 
MyStack=[]
def Push(rollno):
    MyStack.append(rollno)
def Pop(MyStack):
 if len(MyStack) > 0:
  MyStack.pop()
 else:
  print("Stack is empty.")
 
Q6. Write add(bookname) and delete() method in python
 to add bookname and remove bookname considering
them to act as push() and pop() operations in stack.
Ans.
MyStack=[]
def add(bname):
    MyStack.append(bname)
def delete(MyStack):
 if len(MyStack) > 0:
  MyStack.pop()
 else:
  print("Stack is empty. There is no book name")
 
Q7. Write addclient(clientname) and remove()
methods in python to add new client and delete
existing client from a list "clientdetail",
considering them to act as push and pop
operations of the stack.

Ans. 
clientdetail=[]
def addclient(cn):
   clientdetail.append(cn)

def remove():
   if len(clientdetail)>0:
      clientdetail.pop()
   else:
      print("Stack is empty")

   
 
 
 
 
 
 

 
 
 

Most Recently Published

File Handling Test 6

 File Handling Test 6 Q1 . Do we need to use the close() function when we use 'with' statement to open a file? Q2. Which mode you wi...

CS - IP Assignment/Worksheet