0% found this document useful (0 votes)
4 views

STACK PROGRAM

The document outlines a stack program in Python that includes two main functions. The first function, Push_element, adds cities and countries not in India with a distance less than 3500 km from Delhi to a stack, while the second function, Pop_element, removes and displays items from the stack, indicating when it is empty. Additionally, a function named Push is defined to add stationary items with a price greater than 75 to a stack and display the count of items added.

Uploaded by

drown2game
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

STACK PROGRAM

The document outlines a stack program in Python that includes two main functions. The first function, Push_element, adds cities and countries not in India with a distance less than 3500 km from Delhi to a stack, while the second function, Pop_element, removes and displays items from the stack, indicating when it is empty. Additionally, a function named Push is defined to add stationary items with a price greater than 75 to a stack and display the count of items added.

Uploaded by

drown2game
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

STACK PROGRAM

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.

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],["Colombo", "Sri Lanka",
3405]]

The stack should contain:

['Naypyidaw', 'Myanmar'],

['Dubai', 'UAE'],

['Colombo', 'Sri Lanka']

The output should be:

['Colombo', 'Sri Lanka']

['Dubai', 'UAE']

['Naypyidaw', 'Myanmar']

Stack Empty
Ans:

2.

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

Ans:

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)

-------------

You might also like