Omid - Challenge 296

data-challenges
advanced-exercises
🔰 Challenge 296: Transformation!
Published

March 24, 2026

Illustration for Omid - Challenge 296

Challenge Description

🔰 Challenge 296: Transformation!

Solutions

library(tidyverse)
library(readxl)

path = "files/200-299/296/CH-296 Transformation.xlsx"
input = read_excel(path, range = "B2:E18")
test  = read_excel(path, range = "I2:K26") %>% arrange(Date, Product)

result = stack(input[c(1:4)]) %>%
  mutate(date = ifelse(str_detect(values, "\\d{5,5}"), values, NA)) %>%
  fill(date) %>%
  mutate(product = ifelse(str_detect(values, "[A-Z]"), values, NA)) %>%
  group_by(date) %>%
  fill(product) %>%
  ungroup() %>%
  filter(values != product & values != date) %>%
  mutate(date = as.Date(as.numeric(date), origin = "1899-12-30") %>% as.POSIXct(),
         values = as.numeric(values)) %>%
  select(Date = date, Product = product, Result = values) %>% 
  arrange(Date, Product)

all.equal(result, test, check.attributes = FALSE)
# TRUE
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Aggregates or ranks values at the relevant grouping level

    • Builds the intermediate columns that drive the final result

    • Parses the text patterns directly instead of relying on manual cleanup

  • 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
import numpy as np

path = "200-299/296/CH-296 Transformation.xlsx"
input = pd.read_excel(path, usecols="B:E", skiprows=1, nrows=16)
test = pd.read_excel(path, usecols="I:K", skiprows=1, nrows=25).sort_values(['Date', 'Product']).reset_index(drop=True)

df = input.melt(value_name='Value')['Value']
df = pd.DataFrame(df)
df['Date'] = df['Value'].where(df['Value'].astype(str).str.startswith('2024')).ffill()
df['Product'] = df['Value'].where(df['Value'].isin(['A', 'B', 'C'])).ffill()
df = df[~df['Value'].isin(df['Date'].dropna().unique()) & ~df['Value'].isin(['A', 'B', 'C'])]
df = df.sort_values(['Date', 'Product']).reset_index(drop=True)
df['Result'] = df['Value'].astype(np.int64)
result = df[['Date', 'Product', 'Result']]

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

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

  • 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 core logic is clear, but the correct transformation pattern is not obvious from the raw input.

  • The challenge combines multiple reshaping, grouping, or parsing steps.