Complete Python Series · Part 28 of 29
Assignment 15-revenue-pipeline
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Revenue Processing Pipeline
Learn to flatten nested collections, filter transactions, transform values, and aggregate results using nested list comprehensions, filter(), map(), and reduce().
Problem statement
NextGen CTO receives course purchase transactions from website purchases, college partnerships, and corporate training. Each channel maintains a separate list of USD transaction amounts. Positive amounts represent purchases, while negative amounts represent refunds. Build a pipeline to calculate the total positive sales converted to INR at a fixed exchange rate of 85 INR per USD.
Task requirements
- Flatten the nested sales lists into a single list using a nested list comprehension.
- Use
filter()and a lambda to retain only amounts greater than zero. - Use
map()and a lambda to multiply each remaining amount by the exchange rate. - Use
reduce()with an initial value of zero to calculate the total. - Print the total using an f-string with two decimal places.
Constraints
- Use a nested list comprehension,
filter(),map(), andreduce()for their respective steps. - Do not use explicit loops or
sum(). - Do not modify the original sales data.
- Use the fixed illustrative exchange rate of 85.
Good luck trying the exercise!
Starter code
from functools import reduce
channel_sales = [
[100, 200, -50], # Website purchases
[150, 300], # College partnerships
[-20, 100, 50] # Corporate training
]
exchange_rate = 85
# TODO: Flatten the nested list using a comprehension
all_sales = []
# TODO: Filter out non-positive transactions
positive_sales = []
# TODO: Convert positive transactions into INR using map()
converted_sales = []
# TODO: Aggregate converted transactions using reduce()
total_revenue = 0
print(f"Total Revenue: INR {total_revenue:.2f}")
Hints
- A nested comprehension can use two
forclauses: one for each channel and one for each transaction within the channel. filter()accepts a function that returns a Boolean; uselambda sale: sale > 0.map()applies a transformation to each item. Convert its iterator to a list usinglist().reduce()combines an accumulator with each item. Supply0as the initializer to handle empty lists.
Solution: NextGen CTO Revenue Processing Pipeline
The solution combines four operations into a simple data processing pipeline: flatten, filter, transform, and aggregate.
from functools import reduce
# Sales transactions from different channels
channel_sales = [
[100, 200, -50], # Website purchases
[150, 300], # College partnerships
[-20, 100, 50] # Corporate training
]
exchange_rate = 85
# Step 1: Flatten transactions using a nested comprehension
all_sales = [
sale
for channel in channel_sales
for sale in channel
]
# Step 2: Filter out refunds and zero-value transactions
positive_sales = list(
filter(lambda sale: sale > 0, all_sales)
)
# Step 3: Convert USD transactions into INR
converted_sales = list(
map(lambda sale: sale * exchange_rate, positive_sales)
)
# Step 4: Calculate total revenue using reduce()
total_revenue = reduce(
lambda total, sale: total + sale,
converted_sales,
0
)
print(f"Total Revenue: INR {total_revenue:.2f}")
Expected output
Total Revenue: INR 76500.00
Solution explanation
- Line 1: Import
reduce()from thefunctoolsmodule. - Lines 4–10: Define the nested sales data and fixed exchange rate.
- Lines 13–17: Flatten the transactions by iterating over each channel and then each sale.
- Lines 20–22: Filter transactions to retain only positive amounts.
list()materializes the filter iterator. - Lines 25–27: Apply the currency conversion to every retained transaction using
map(). - Lines 30–34: Aggregate converted amounts with
reduce(), starting from zero. - Line 36: Display the total using an f-string with two decimal places.
Intermediate results
Test cases
The exchange rate is hypothetical. The result represents gross positive sales, not net revenue, because refunds are excluded rather than deducted.