CBSE · 083Class XII · 2023Section C3 marks

Question 30(2)

30 March 2023 · Computer Science (083)

Write a function in Python, Push(Vehicle) where Vehicle is a dictionary containing details of vehicles — {Car_Name: Maker}. The function should push the name of every car manufactured by 'TATA' (in any case — Tata, TaTa, etc.) onto the stack.

For example, if the dictionary contains :

python
Vehicle = {"Santro": "Hyundai", "Nexon": "TATA", "Safari": "Tata"}

The stack should contain : Safari, Nexon

Answer

python
stack = []

def Push(Vehicle):
    for v_name in Vehicle:
        if Vehicle[v_name].upper() == "TATA":
            stack.append(v_name)

Explanation

Iterates over the dictionary keys, normalises each value with .upper(), and appends qualifying keys to the stack.