Omid - Challenge 75

data-challenges
advanced-exercises
🔰 In the Question table, the order date for different products is provided (determined by positive quantity).
Published

March 24, 2026

Illustration for Omid - Challenge 75

Challenge Description

🔰 In the Question table, the order date for different products is provided (determined by positive quantity).

Solutions

library(tidyverse)
library(readxl)

path = "files/CH-75 Table Transformation.xlsx"
input = read_xlsx(path, range = "B2:E20")
test  = read_xlsx(path, range = "I2:J5") %>%
  mutate(`AVG Delivery Time` = round(`AVG Delivery Time`, 2))

orders = input %>%
  filter(Quantity > 0)
deliveries = input %>%
  filter(Quantity < 0)

together = orders %>%
  left_join(deliveries, by = c("Order ID" = "Order ID")) %>%
  mutate(Days = as.numeric(Date.y - Date.x)) %>%
  mutate(total_quantity = -sum(Quantity.y), .by = Product.x) %>%
  summarise(`AVG Delivery Time` = round(sum(Days * -Quantity.y) / min(total_quantity),2), 
            .by = Product.x) %>%
  arrange(Product.x) %>%
  rename(Product = Product.x)

identical(together, test)
# [1] TRUE
  • Logic:

    • Aggregates or ranks values at the relevant grouping level

    • Builds the intermediate columns that drive the final result

  • 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 = "CH-75 Table Transformation.xlsx"
input = pd.read_excel(path, usecols="B:E", skiprows=1, nrows=18)
test = pd.read_excel(path, usecols="I:J", skiprows=1, nrows=3)
test["AVG Delivery Time"] = round(test["AVG Delivery Time"], 2)

orders = input[input["Quantity"] > 0].reset_index(drop=True)
deliveries = input[input["Quantity"] < 0].reset_index(drop=True)

together = pd.merge(orders, deliveries, on="Order ID", how="left")
together["Date_y"] = pd.to_datetime(together["Date_y"])
together["Date_x"] = pd.to_datetime(together["Date_x"])
together["Days"] = (together["Date_y"] - together["Date_x"]).dt.days.astype(int)
together["total_quantity"] = together.groupby("Product_x")["Quantity_y"].transform("sum").multiply(-1)

together["avg_delivery_time"] = (together["Days"] * together["Quantity_y"] * -1) / together["total_quantity"]
together = together.groupby("Product_x")["avg_delivery_time"].sum().round(2).reset_index()
together.columns = test.columns

print(together.equals(test)) # True
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

  • 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.