Program Console Vignaankosh.com
Execution Panel
Console is empty.
Build mini analytics using functional programming is an interactive PYTHON dry run visualizer from the Special programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.
This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.
from functools import reduce
# Sample sales data
sales_data = [
{"product": "A", "category": "Electronics", "price": 100, "quantity": 10},
{"product": "B", "category": "Clothing", "price": 50, "quantity": 20},
{"product": "C", "category": "Electronics", "price": 200, "quantity": 5},
{"product": "D", "category": "Clothing", "price": 30, "quantity": 15},
{"product": "E", "category": "Home", "price": 80, "quantity": 8}
]
# 1. Total revenue
total_revenue = reduce(lambda acc, s: acc + s["price"] * s["quantity"], sales_data, 0)
# 2. Revenue by category
revenue_by_cat = reduce(
lambda acc, s: {**acc, s["category"]: acc.get(s["category"], 0) + s["price"] * s["quantity"]},
sales_data, {}
)
# 3. Products with revenue > 500
high_revenue = list(filter(lambda s: s["price"] * s["quantity"] > 500, sales_data))
# 4. Apply 10% discount to Electronics category
discounted = list(map(
lambda s: {**s, "price": s["price"] * 0.9} if s["category"] == "Electronics" else s,
sales_data
))
# 5. Average price by category
avg_price_by_cat = reduce(
lambda acc, s: {**acc, s["category"]: (acc.get(s["category"], (0,0))[0] + s["price"],
acc.get(s["category"], (0,0))[1] + 1)},
sales_data, {}
)
avg_price_by_cat = {k: v[0]/v[1] for k, v in avg_price_by_cat.items()}
print(f"1. Total Revenue: ${total_revenue}")
print(f"2. Revenue by Category: {revenue_by_cat}")
print(f"3. High Revenue Products (>$500): {[(s['product'], s['price']*s['quantity']) for s in high_revenue]}")
print(f"4. Electronics after 10% discount: {[s['product'] for s in discounted if s['category']=='Electronics']}")
print(f"5. Average Price by Category: {avg_price_by_cat}")