Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. 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 5

 Data Structure Test 5

Q1. Write queueadd(clientname) and queuedel(clientname) methods in python to add a new client and delete a client from a list of Client Names, considering them to act as an insert and delete operations of the queue data structure.

Ans.

cn=[]
def queueadd(cn):
cname=input("Enter Client Name")
cn.append(cname)
def queuedel(cn):
if(len(cn)==0):
print("Queue is Empty")
else:
print("Deleted element is", cn[0])
del cn[0]
#you can call the following functions to verify
the above code
queueadd(cn)
queueadd(cn)
queueadd(cn)
queuedel(cn)
print(cn)

Q2. Write queueadd(pid) and queuedel(pid) methods in python to add a new product  and delete a product  from a list of product id, considering them to act as an insert and delete operations of the queue data structure. (Consider product id of integer type)
Ans.
pid=[]
def queueadd(cn):
prid=int(input("Enter Product id"))
cn.append(prid)
def queuedel(pid):
if(len(pid)==0):
print("Queue is Empty")
else:
print("Deleted element is", pid[0])
del pid[0]
#you can call the following functions to verify
the above code
queueadd(pid)
queueadd(pid)
queueadd(pid)
queuedel(pid)
print(pid)


Q3. Write add(book) and del1(book) methods in python to add a new book and delete a book  from a list of book name, considering them to act as an insert and delete operations of the queue data structure. 
Ans. 
book=[]
def add(book):
bn=input("Enter book name")
book.append(bn)
def del1(book):
if(len(book)==0):
print("Queue is Empty")
else:
print("Deleted element is", book[0])
del book[0]
#you can call the following functions to verify
#the above code
add(book)
add(book)
add(book)
del1(book)
print(book)

Q4. Write addq(Employee) and delq(Employee) methods in python to add a new Employee Name  and delete an Employee  from a list of Employee name, considering them to act as an insert and delete operations of the queue data structure. 
Ans. 
emp=[]
def addq(emp):
en=input("Enter employee name")
emp.append(en)
def delq(emp):
if(len(emp)==0):
print("Queue is Empty")
else:
print("Deleted element is", emp[0])
del emp[0]
#you can call the following functions to verify
#the above code
addq(emp)
addq(emp)
addq(emp)
delq(emp)
print(emp)

Q5. Write adding(Rollno) and deleting(Rollno) methods in python to add a new Roll Number and delete Roll Number  from a list of Roll Number, considering them to act as an insert and delete operations of the queue data structure. (Roll number is of type integer
Ans. 

rno=[]
def adding(rno):
rn=int(input("Enter Roll Number"))
rno.append(rn)
def deleting(rno):
if(len(rno)==0):
print("Queue is Empty")
else:
print("Deleted element is", rno[0])
del rno[0]
#you can call the following functions to verify
#the above code
adding(rno)
adding(rno)
adding(rno)
deleting(rno)
print(rno)

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

   
 
 
 
 
 
 

 
 
 

Data Structure Test 2

 Data Structure Test 2
Q1. Organization of data means ______.
Q2. Write the full form of the following:
a. LIFO
b. FIFO
Q3. Which data structure is represented as FIFO?
Q4. Insertion into stack is called ______ (push/pop)
Q5. Giving printing command to printer is an example of _____ 
    (stack/queue)
Q6. Reversing a number or a word/string is an example of 
    ______(stack or queue)
Q7. In stack addition or removal of elements takes place at ___ 
    (one/both) end of the list.
Q8. In queue, addition of elements take place at one end and 
    removal of elements takes place at other end. (T/F)
Q9. If the elements "A", "B", "C" are added in the queue in the 
    following order,
    first A then B and in last C.
    In what order, it will come out of 
    queue?
Q10. _________ function is used to add an  element in stack.


SOLUTIONS

Ans 1. Data Structure

Ans 2. 
    a. Last In First Out
    b. First In First Out

Ans 3. Queue

Ans 4. Push

Ans 5. Queue

Ans 6. Stack

Ans 7. One

Ans 8. True

Ans 9. A, B, C

Ans 10. Append


Class XII : Data Structure Test 1

 

Data Structure Test 1

Q1. What do you mean by data structure?

Q2. Name any one linear data structure in python.

Q3. Python list are ______________ in Nature (static/dynamic)

Q4. “Lists are dynamic in nature” What do you mean by this statement?

Q5. What do you mean by Stack?

Q6. Name the end where we add or remove element in stack.

Q7. What is the full form of LIFO?

Q8. Give two example of stack from daily life.

Q9. Name two operations performed in stack.

Q10. What is the order of inserting elements in the following stack?

 

 

 

Ans 1. A data structure is a way of storing, organizing and retrieving data in a computer.

Ans 2. List

Ans 3. Dynamic

Ans 4. This means that list can be grown or shrink in size means elements can be deleted or added in list.

Ans 5. A stack is a linear data structure where elements can be inserted or deleted at one end.

Ans 6. Top

Ans 7. Last in First Out

Ans 8. A pile of book, a stack of carom coins etc.

Ans 9. Push and Pop

Ans 10. Order is: A, B, C, D

 

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