DEV Community

Cover image for Day 21 of My AI & Data Mastery Journey: From Python to Generative AI
Nitin-bhatt46
Nitin-bhatt46

Posted on

Day 21 of My AI & Data Mastery Journey: From Python to Generative AI

Problem 1:

You’re analyzing a shopping mall’s daily transaction list. Each item has (price, quantity) in a list of tuples.
You must calculate the total bill for each transaction and filter out the transactions where total bill < ₹1000.

Input:

transactions = [(200, 2), (100, 5), (450, 3), (150, 1), (600, 2)]

Expected Output:

[1350, 1200]
Enter fullscreen mode Exit fullscreen mode

Hint:

Use map() with lambda to calculate total bill per transaction.

Then use filter() to keep only totals >= 1000.

Problem 2 :

You have employee performance data as a list of (employee_name, rating_list) tuples.
You must compute average rating for each employee, then filter out employees with avg rating < 4.

Input:

data = [
    ("Nitin", [4, 5, 3, 4]),
    ("Ravi", [5, 5, 5, 4]),
    ("Simran", [2, 3, 2, 4]),
    ("Meena", [4, 4, 4, 4])
]
Enter fullscreen mode Exit fullscreen mode

Expected Output:

[('Ravi', 4.75), ('Meena', 4.0)]
Enter fullscreen mode Exit fullscreen mode

Hint:

Use map() to calculate average rating for each employee.

Use filter() to keep employees with avg ≥ 4.

Round values to 2 decimals if needed.

DEBUGGING :-

Debug 1 — Square and Filter Even Numbers

Broken Code:

nums = [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

even_squares = map(lambda x: x**2 if x%2==0, nums)
print(list(even_squares))

Expected Output:

[4, 16, 36]
Enter fullscreen mode Exit fullscreen mode

Hint:
You can’t use if directly like this in a lambda for conditional filtering — fix it using filter() or a conditional expression.

Debug 2 — Filter Expensive Products

Broken Code:

prices = {"laptop":50000, "mouse":800, "keyboard":1500, "monitor":9000}

expensive = list(filter(lambda x: prices[x] > 5000, prices.items()))
print(expensive)
Enter fullscreen mode Exit fullscreen mode

Expected Output:

[('laptop', 50000), ('monitor', 9000)]
Enter fullscreen mode Exit fullscreen mode

Hint:
Remember that .items() gives (key, value) pairs.
Use tuple unpacking in your lambda.

Top comments (0)