library(tidyverse)
library(readxl)
path = "files/300-399/305/CH-305 Advanced Calculation.xlsx"
input = read_excel(path, range = "B2:D19")
test = read_excel(path, range = "H2:I5")
result = input %>%
summarise(Index = diff(range(Sales)/mean(Sales[!Sales %in% range(Sales)])), .by = Product)
all.equal(result, test)
# wrong answer for product AOmid - Challenge 305
data-challenges
advanced-exercises
🔰 For each product in the question table, calculate the measure column using the following formula.

Challenge Description
🔰 For each product in the question table, calculate the measure column using the following formula.
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
Applies the rule iteratively until the output stabilizes
Strengths:
- The R solution stays close to the workbook rule and keeps the transformation compact.
Areas for Improvement:
- The code assumes the sheet structure and source ranges remain stable.
Gem:
- The strongest part of the solution is choosing the right intermediate representation before shaping the final output.
import pandas as pd
path = "300-399/305/CH-305 Advanced Calculation.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=17)
test = pd.read_excel(path, usecols="H:I", skiprows=1, nrows=4)
result = input.groupby('Product')['Sales'].apply(
lambda x: (x.max() - x.min()) / x.drop([x.idxmin(), x.idxmax()]).mean()
)
print(result)
# Different result for Product ALogic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
Applies the rule iteratively until the output stabilizes
Strengths:
- The Python version follows the same rule in a direct dataframe-oriented implementation.
Areas for Improvement:
- The code assumes the workbook layout remains stable, so any sheet redesign would require small adjustments.
Gem:
- The implementation stays close to the original workbook rule instead of adding unnecessary abstraction.
Difficulty Level
This task is moderate:
- The business rule is readable, but the workbook still requires careful implementation to reach the expected layout.