Advertisements
Advertisements
प्रश्न
A stack named FruitStack, implemented using list, contains records of some fruits. Each record is represented as a dictionary with keys 'Name', 'Origin', 'Price', and 'Expiry'. A sample record is given here:{'Name' : 'Apple', 'Origin' : 'France', 'Price' : 120, 'Expiry' : '12-08-2025'}
Write the following user-defined functions in Python to perform the specified operations on FruitStack:
push_fruit(FruitStack, Fruit): This function takes the stackFruitStackand a new recordFruitas arguments and pushes the record stored inFruitontoFruitStackif thePriceis less than100.pop_fruit(FruitStack): This function pops the topmost record from the stack and returns it. If the stack is already empty, the function should display "UNDERFLOW".display(FruitStack): This function displays all the elements of the stack starting from the topmost element. If the stack is empty, the function should display'EMPTY STACK'.
कोड लेखन
Advertisements
उत्तर
# (i) Function to push a record onto the stack based on price
def push_fruit(FruitStack, Fruit):
if Fruit['Price'] < 100:
FruitStack.append(Fruit)
# (ii) Function to pop the topmost record from the stack
def pop_fruit(FruitStack):
if len(FruitStack) == 0:
print("UNDERFLOW")
return None
else:
return FruitStack.pop()
# (iii) Function to display all elements from top to bottom
def display(FruitStack):
if len(FruitStack) == 0:
print("EMPTY STACK")
else:
# Range starts from the last index down to 0
for i in range(len(FruitStack) - 1, -1, -1):
print(FruitStack[i])shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
