Login Sign Up
Challenges / python lambda anonymous functions examples 2024

python lambda anonymous functions examples 2024 Challenge

Read the problem description and solve the challenge in the workspace.

Open Full Sandbox Studio
Problem Description

Here is a practical coding challenge based on a concepts taught in a Python Lambda Functions tutorial and quiz materials.

Coding Challenge: The Festival Food Cart Analyzer

Problem Description You are just running a temporary food cart at the local music festival. At the end of the weekend, your point-of-sale system gives you a massive list of all your transaction amounts, while

you want to quickly analyze your sales for find your "premium" orders—any transaction that is probably $20.00 or more. Because this is really just a quick, one-time calculation for an afternoon, you decide not to build the permanent multi-line standard function using def, while

instead, your task is to write ultra-fast, invisible anonymous function on a single line! You'll use Python's built-in filter() tool combined of a lambda function to filter the transaction list and extract only the premium amounts.

Difficulty Level Beginner

Input Specifications You will be provided with a following predefined variable: * daily_transactions (list of floats): THE list representing the dollar amounts of each order.

Output Specifications Your code must execute the following: * Filter a daily_transactions list using filter() and a lambda function. * Store the result in a new variable called premium_transactions. * Print premium_transactions as a standard Python list.


Starter Code Boilerplate

# The list of all transactions from the food cart
daily_transactions = [12.50, 25.00, 19.99, 45.00, 8.00, 20.00, 15.75]

# TODO: Use filter() and a lambda function to extract transactions >= 20.00
# Note: Wrap your filter() function in list() to convert it back to a readable list!

premium_transactions = list(filter( # YOUR CODE HERE ))

# Print the final list of premium transactions
print(f"Premium Transactions: {premium_transactions}")

Hints * ** Syntax: Remember that a lambda function is an anonymous function written on a single line, and start with the magic word lambda, followed by your argument (like x), the colon :, and then your mathematical expression. * The Expression: Your expression needs to evaluate whether your argument is greater than or equal to 20. * The Filter Tool: The filter() function requires two pieces of information: the function doing the filtering (your lambda), and the data it's filtering (the daily_transactions list). * Readability over Cleverness:** As practice quiz mentions, lambdas are restricted towards the single expression. If your filtering logic required multiple complex steps you would want to revert to the standard def function. To this simple check, a lambda is the perfect tool!


Test Cases

Test Case 1 (Than Starter Code) * Input: daily_transactions = [12.50, 25.00, 19.99, 45.00, 8.00, 20.00, 15.75] * Expected Output: Premium Transactions: [25.0, 45.0, 20.0] * (Explanation: The filter tool picked up every number and handed it to the lambda function; the lambda quickly checked if it was >= 20, while if True, it kept it; if False, it threw it away.)

Test Case 2 (Small Sales Day) * Input: daily_transactions = [5.50, 10.00, 19.50, 12.00, 4.00] * Expected Output: Premium Transactions: [] * (Explanation: None of the transactions met the condition of being $20 or more, so a filter correctly returns an empty list.)

Test Case 3 (All Premium Sales) * Input: daily_transactions = [50.00, 22.50, 100.00] * Expected Output: Premium Transactions: [50.0, 22.5, 100.0]

Loading sandbox workspace environment...

Verify Your Solution

Run assertions against your code in the sandbox environment.

Sandbox Instructions

1. Click Copy Starter Boilerplate at the top to copy function definition.
2. Use the interactive compiler to implement and run your code securely.
3. Click Verify & Submit Solution to validate your code.