Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views6 pages

Class XII Stack Worksheet 4 - 12

The document contains a series of Python programming exercises focused on stack operations and data manipulation. It includes tasks such as pushing and popping elements from stacks based on specific criteria, processing lists and dictionaries, and implementing functions for various operations. Each exercise provides sample data and expected outputs to guide the implementation of the required functions.

Uploaded by

Shashank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Class XII Stack Worksheet 4 - 12

The document contains a series of Python programming exercises focused on stack operations and data manipulation. It includes tasks such as pushing and popping elements from stacks based on specific criteria, processing lists and dictionaries, and implementing functions for various operations. Each exercise provides sample data and expected outputs to guide the implementation of the required functions.

Uploaded by

Shashank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CLASS XII – STACK WORKSHEET 4 - 12

1)A list, NList contains 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.

Nlist=[['Bahrain','Iran',5000],['Kathmandu','Nepal',2500],['Indore','India',2250]]

travel=[]

def Push_element(Nlist):

for i in Nlist:

if i[1]!='India' and i[2]<3500:

travel.append([i[0],i[1]])

def Pop_element():

while len(travel):

print(travel.pop())

else:

print('Stack Empty')

Push_element(Nlist)

Pop_element()

2) Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument
and displays the names (in uppercase)of the places whose names are longer than 5 characters. For
example, Consider the following dictionary PLACES={1:"Delhi",2:"London",3:"Paris",4:"New
York",5:"Doha"}

PLACES={1:"Delhi",2:"London",3:"Paris",4:"New York",5:"Doha"}

def pop(PLACES):

for i in PLACES:

if len(PLACES[i])>5:

print(PLACES[i].upper())

pop(PLACES)
3) Write a function, lenWords(STRING), that takes a string as an argument and returns a tuple
containing length of each word of a string. For example, if the string is "Come let us have some fun",
the tuple will have (4, 3, 2, 4, 4, 3)

string=('Come let us have some fun')

tup=()

def lenWords():

a=string.split()

for i in a:

tup=tup + (len(i),)

print(tup)

lenWords()

OR
def lenWORDS(STRING):

t=()

count=0

for i in STRING:

if i==" ":

t=t+(count,)

count=0

else:

count=count+1

print(t)

STRING="Come let us have some fun "

lenWORDS(STRING)

4) Write a function INDEX_LIST(L), where L is the list of elements passed as argument to the function.
The function returns another list named „indexList‟ that stores the indices of all Non-Zero Elements
of L. For example: If L contains [12,4,0,11,0,56] The indexList will have - [0,1,3,5]

L=[12,4,0,11,0,56]

indexList=[]

def INDEX_LIST(L):

for i in range(len(L)):

if L[i]!=0:
indexList.append(i)

print(indexList)

INDEX_LIST(L)

5) 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.

status=[[Rajesh,9675845673,Chennai],[Kumar, 9575428281,Goa]]

status1

def Push_element():

for i in status:

if i[2]=='Goa':

L=[i[0],i[1]]

status1.append(L)

def Pop_element():

num=len(status1)

while len(status1)!=0:

dele=status1.pop()

print(dele)

num=num-1

else:

print('Stack Empty')

6) 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.

stackItem={#input}

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 th stack is: ',count)

7) Julie has created a dictionary containing names and marks as key value pairs of 6 students. Write a
program, with separate user defined functions to perform the following operations: ● Push the keys
(name of the student) of the dictionary into a stack, where the corresponding value (marks) is
greater than 75. ● Pop and display the content of the stack. For example: If the sample content of the
dictionary is as follows: R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82} The output
from the program should be: TOM ANU BOB OM

R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}

def Push(S,N):

S.append(N)

def Pop(S):

if S!=[]:

return S.pop()

else:

return 'None'

S=[]

for k in R:

if R[k]>=75:

Push(S,k)

while True:

if S!=[]:

print(Pop(S),end=' ')

else:

break

8) Alam has a list containing 10 integers. You need to help him create a program with separate user
defined functions to perform the following operations based on this list. ● Traverse the content of
the list and push the even numbers into a stack. ● Pop and display the content of the stack. For
Example: If the sample Content of the list is as follows: N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be: 38 22 98 56 34 12

def Push(ST,k):
ST.append(k)

def Pop(ST):

if ST!=[]:

return ST.pop()

else:

return 'None'

N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]

ST=[]

for k in N:

if k%2==0:

print(Push(ST,k))

while True:

if ST!=[]:

print(Pop(ST),end=' ')

else:

break

9) Write a function LShift(Arr,n) in Python, which accepts a list Arr of numbers and n is a numeric
value by which all elements of the list are shifted to left. Sample Input Data of the list Arr=
[ 10,20,30,40,12,11], n=2 Output Arr = [30,40,12,11,10,20].

def LShift(Arr,n):

for k in range(n):

a=Arr[0]

for i in range(len(Arr)-1):

Arr[i]=Arr[i+1]

Arr[-1]=a

print(Arr)

LShift([10,20,30,40,12,11],2)

10) 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.

def Push(Arr):

s=[]

for i in range(len(Arr)):

if Arr[i]%5==0:

s.append(Arr[i])

if len(s)==0:

print('Empty Stack')

else:

print(s)

def Pop(Arr):

if len(Arr)==0:

print('Underflow')

else:

L=len(Arr)

val=Arr[L-1]

print(val)

Arr.pop(L-1)

Arr=[2,8,15,3,5,76,35]

Push(Arr)

Pop(Arr)

You might also like