Computer Science

Two lists Lname and Lage contain name of person and age of person respectively. A list named Lnameage is empty. Write functions as per details given below:

  1. Push_na(): It will push the tuple containing pair of name and age from Lname and Lage whose age is above 50.
  2. Pop_na(): It will remove the last pair of name and age and also print name and age of the removed person. It should also print "underflow" if there is nothing to remove.

For example, the two lists have the following data:

Lname=['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush' ]
Lage= [45, 23, 59, 34, 51, 43]

After Push_na() the Lnameage Stack contains:

[('Raju', 59), ('Amit', 51)]

The output of first execution of Pop_na() is:

The name removed is Amit
The age of person is 51

Python

Python Stack

6 Likes

Answer

def Push_na(Lname, Lage, Lnameage):
    for i in range(len(Lname)):
        if Lage[i] > 50:
            Lnameage.append((Lname[i], Lage[i]))

def Pop_na(Lnameage):
    if Lnameage:
        removed_name, removed_age = Lnameage.pop()
        print("The name removed is", removed_name)
        print("The age of person is", removed_age)
    else:
        print("Underflow - Nothing to remove")

Lname = ['Narender', 'Jaya', 'Raju', 'Ramesh', 'Amit', 'Piyush']
Lage = [45, 23, 59, 34, 51, 43]
Lnameage = []

Push_na(Lname, Lage, Lnameage)
print("After Push_na() the Lnameage Stack contains:")
print(Lnameage)

print("\nThe output of first execution of Pop_na() is:")
Pop_na(Lnameage)

Output

After Push_na() the Lnameage Stack contains:
[('Raju', 59), ('Amit', 51)]

The output of first execution of Pop_na() is:
The name removed is Amit
The age of person is 51

Answered By

2 Likes


Related Questions