Omid - Challenge 65

data-challenges
advanced-exercises
🔰 Transformation In the question table, the total costs for different date ranges are provided.
Published

March 24, 2026

Illustration for Omid - Challenge 65

Challenge Description

🔰 Transformation In the question table, the total costs for different date ranges are provided.

Solutions

library(tidyverse)
library(readxl)

input = read_excel("files/CH-065 Transformation.xlsx", range = "B2:D6")
test  = read_excel("files/CH-065 Transformation.xlsx", range = "F2:G12")

result = input %>%
  mutate(seq = map2(From, TO, ~seq.POSIXt(.x, .y, by = "day")),
         len = map(seq, ~length(.x)),
         `AVG Cost` = map2_dbl(len, Cost, ~ .y / .x)) %>%
  select(-c(From, TO, len, Cost)) %>%
  unnest(cols = c(seq)) %>%
  select(Date = seq, `AVG Cost`)

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

    • Reads the workbook ranges needed for the challenge

    • 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

input = pd.read_excel("CH-065 Transformation.xlsx", sheet_name="Sheet1", usecols="B:D", skiprows=1, nrows = 4)
test = pd.read_excel("CH-065 Transformation.xlsx", sheet_name="Sheet1", usecols="F:G", skiprows=1, nrows = 10)

result = input.assign(seq = input.apply(lambda row: pd.date_range(start=row['From'], end=row['TO'], freq='D'), axis=1),
                      len = (input['TO'] - input['From'] + pd.Timedelta(days=1)).dt.days,
                      avg_cost = input['Cost'] / ((input['TO'] - input['From'] + pd.Timedelta(days=1)).dt.days))\
                    .explode('seq')\
                    .drop(columns=['From', 'TO', 'len', 'Cost'])\
                    .rename(columns={'seq': 'Date', 'avg_cost': 'AVG Cost'})\
                    .astype({'AVG Cost': 'int64'})\
                    .reset_index(drop=True)

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

    • Reads the workbook ranges needed for the challenge

    • Builds the intermediate columns that drive the final result

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