[go: up one dir, main page]

0% found this document useful (0 votes)
12 views5 pages

Data Structures (Practice Ques. With Sol.)

The document contains a series of programming questions focused on data structures, specifically stacks and queues, using Python. It includes tasks such as pushing and popping elements based on specific conditions, evaluating expressions, and manipulating lists and dictionaries. Additionally, it provides example solutions for each question, demonstrating the implementation of user-defined functions.
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)
12 views5 pages

Data Structures (Practice Ques. With Sol.)

The document contains a series of programming questions focused on data structures, specifically stacks and queues, using Python. It includes tasks such as pushing and popping elements based on specific conditions, evaluating expressions, and manipulating lists and dictionaries. Additionally, it provides example solutions for each question, demonstrating the implementation of user-defined functions.
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/ 5

DATA STRUCTURES

Question 1.
A list, NListcontains following record as list elements:
[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the following user defined functions
in Python to perform the specified operations on the stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes a list object
containing name of the city and country, which are not in India and distance is less than
3500 km from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays them. Also, the function
should display “Stack Empty” when there are no elements in the stack.
For example: If the nested list contains the following data:
NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
The stack should contain:
['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be: ['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar'] Stack Empty

Question 2.
What will the following expression be evaluated to in Python?
print(15.0 / 4 + (8 + 3.0))
(a) 14.75 (b)14.0 (c) 15 (d) 15.5

Question 3.
A list contains following record of a customer: [Customer_name, Phone_number, City]
Write the following user defined functions to perform given operations on the stack named ‘status’:
(i) Push_element() - To Push an object containing name and
Phone number of customers who live in Goa to the stack
(ii) Pop_element() - To Pop the objects from the stack and display them. Also, display “Stack
Empty” when there are no elements in the stack.
For example:
If the lists of customer details are:
[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”]
[“Murugan”,”77777777777”,”Cochin”] [“Ashmit”, “1010101010”,”Goa”]
The stack should contain [“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
The output should be: [“Ashmit”,”1010101010”]
Data Structures (Practice Questions) Page 1 of 5
[“Gurdas”,”9999999999”]
Stack Empty
OR
Write a function in Python, Push(SItem) where , SItem is a dictionary containing the details of stationary
items– {Sname:price}.
The function should push the names of those items in the stack who have price greater than 75. Also
display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain Notebook
Pen

The output should be:


The count of elements in the stack is 2

Question 4.
Evaluate the following expressions:
a) 6 * 3 + 4**2 // 5 – 8
b) 10 > 5 and 7 > 12 or not 18 > 3

Question 5.
Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all numbers
divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one element,
otherwise display appropriate error message.
OR
Write a function in Python POP(Arr), where Arr is a stack implemented by a list of numbers. The function
returns the value deleted from the stack.

Question 6.
Write a function in Python, INSERTQ(Arr,data) and DELETEQ(Arr) for performing insertion and
deletion operations in a Queue. Arr is the list used for implementing queue and data is the value to
be inserted.

OR

Write a function in python, MakePush(Package) and MakePop(Package) to add a new Package and
delete a Package from a List of Package Description, considering
them to act as push and pop operations of the Stack data structure.

Question 7.
Write a suitable Python statement for each of the following tasks using built-in functions/methods
only:
i To delete an element Mumbai:50 from Dictionary D.
ii To display words in a string S in the form of a list
OR
Write a Python Program to display alternate characters of a string
Data Structures (Practice Questions) Page 2 of 5
my_str.
For example, if my_str= "Computer Science" The output should be Cmue cec

Question 8.
Given a Dictionary Stu_dictcontaining marks of students for three
test-series in the form Stu_ID:(TS1, TS2, TS3) as key-value pairs.
Write a Python program with the following user-defined functions to
perform the specified operations on a stack named Stu_Stk

(i) Push_elements(Stu_Stk, Stu_dict): It allows pushing


IDs of those students, from the dictionary Stu_dictinto the stack
Stu_Stk, who have scored more than or equal to 80 marks in the TS3
Test.
(ii) Pop_elements(Stu_Stk): It removes all elements present
inside the stack in LIFO order and prints them. Also, the function
displays 'Stack Empty' when there are no elements in the stack.
Call both functions to execute queries.

For example:
If the dictionary Stu_dictcontains the following data:
Stu_dict ={5:(87,68,89), 10:(57,54,61), 12:(71,67,90),
14:(66,81,80), 18:(80,48,91)}

After executing Push_elements(), Stk_ID should contain


[5,12,14,18]

After executing Pop_elements(), The output should be:


18
14
12
5
Stack Empty

Data Structures (Practice Questions) Page 3 of 5


Solution 1

Solution 2.
(a) 14.75

Solution 3.
status=[]
def Push_element(cust): if cust[2]=="Goa":
L1=[cust[0],cust[1]] status.append(L1)
def Pop_element (): num=len(status)
while len(status)!=0: dele=status.pop()
print(dele) num=num-1
else:
print("Stack Empty")

OR
stackItem=[]
def Push(SItem): count=0
for k in SItem:
if (SItem[k]>=75): stackItem.append(k) count=count+1
print("The count of elements in the stack is : ", count)

Solution 4.
a) 13
b) False

Solution 5.
def PUSH(Arr,value): s=[]
for x in range(0,len(Arr)): if
Arr[x]%5==0:
s.append(Arr[x])
if len(s)==0:
Data Structures (Practice Questions) Page 4 of 5
print("Empty Stack") else:
print(s)
OR
def popStack(st) :
# If stack is empty if
len(st)==0:
print("Underflow") else:
L = len(st) val=st[L-1] print(val) st.pop(L-1)

Solution 6.
def INSERTQ(Arr):
data=int(input("enter data to be inserted: ")) Arr.append(data)
def DELETEQ(Arr): if (Arr==[]):
print( "Queue empty") else:
print ("Deleted element is: ",Arr[0]) del(Arr[0])

OR
def MakePush(Package): a=int(input("enter package title : "))
Package.append(a)
def MakePop(Package): if (Package==[]):
print( "Stack empty") else:
print ("Deleted element:",Package.pop())

Solution 7.
i. del D['Mumbai']
ii. print(S.split())
OR
my_str = "Computer Science" alternate_chars = my_str[::2] print(alternate_chars)

Solution 8.
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 Stu_ID, marks in Stu_dict.items():
if marks[2]>=80:
Stu_Stk.append(Stu_ID)
def Pop_elements(Stu_Stk): while len(Stu_Stk)>0:
print(Stu_Stk.pop())
if not Stu_Stk:
print('Stack Empty')
Push_elements(Stu_Stk, Stu_dict) Pop_elements(Stu_Stk)

Data Structures (Practice Questions) Page 5 of 5

You might also like