Omid - Challenge 251

data-challenges
advanced-exercises
🔰 Table Transformation!
Published

March 24, 2026

Illustration for Omid - Challenge 251

Challenge Description

🔰 Table Transformation!

Solutions

library(tidyverse)
library(readxl)

path = "files/200-299/251/CH-251 Table Transformation.xlsx"
input = read_excel(path, range = "B3:D16", col_names = F) %>% as.matrix()
test = read_excel(path, range = "F2:H9")

result = input %>%
  t() %>%
  matrix(ncol = 1) %>%
  trimws() %>%
  as.data.frame() %>%
  mutate(group = cumsum(str_length(V1) == 5)) %>%
  slice_head(n = 3, by = group) %>%
  mutate(l = row_number(), .by = group) %>%
  mutate(
    l = case_when(
      l == 1 ~ "Date",
      l == 2 ~ "Product",
      l == 3 ~ "Quantity"
    )
  ) %>%
  pivot_wider(names_from = l, values_from = V1) %>%
  mutate(
    Date = janitor::excel_numeric_to_date(as.numeric(Date)) %>% as.POSIXct(),
    Quantity = as.numeric(Quantity)
  ) %>%
  select(-group)

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

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

    • 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
from openpyxl import load_workbook
from datetime import datetime

path = "200-299/251/CH-251 Table Transformation.xlsx"
input_df = pd.read_excel(path, header=None, usecols="B:D", skiprows=2, nrows=14)
input_matrix = input_df.values
test = pd.read_excel(path, usecols="F:H", skiprows=1, nrows=7)

flat = input_matrix.flatten()
df = pd.DataFrame({'Value': flat})
df['Group'] = df['Value'].apply(lambda x: isinstance(x, (datetime, np.datetime64))).cumsum()
df['Field'] = df.groupby('Group').cumcount().map({0: 'Date', 1: 'Product', 2: 'Quantity'})
df = df.dropna().pivot(index='Group', columns='Field', values='Value').reset_index(drop=True)
df['Quantity'] = df['Quantity'].astype(int)
df['Date'] = pd.to_datetime(df['Date'])

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

    • Reads the workbook ranges needed for the challenge

    • Reshapes the data into the grain required by the task

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