Omid - Challenge 284

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

March 24, 2026

Illustration for Omid - Challenge 284

Challenge Description

🔰 Challenge 284: Transformation!

Solutions

library(tidyverse)
library(readxl)
library(janitor)

path = "files/200-299/284/CH-284 Transformation.xlsx"
input = read_excel(path, range = "B2:E10")
test  = read_excel(path, range = "I2:J18")

result = input %>% 
  mutate(row = row_number() %% 2,
         nr = (row_number() + 1) %/% 2) %>%
  pivot_longer(-c(row, nr), names_to = "col") %>%
  pivot_wider(names_from = row, values_from = value) %>%
  select(Date = `1`, Value = `0`) %>%
  mutate(Date = excel_numeric_to_date(Date) %>% as.POSIXct()) 

all.equal(result, test, check.attributes = FALSE)
# > [1] 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

  • 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/284/CH-284 Transformation.xlsx"

input = pd.read_excel(path, usecols="B:E", skiprows=1, nrows=9)
test = pd.read_excel(path, usecols="I:J", skiprows=1, nrows=17)

input['row'] = (np.arange(len(input)) + 1) % 2
input['nr'] = ((np.arange(len(input)) + 2) // 2)

result = (
    input.melt(id_vars=['row', 'nr'], var_name='col', value_name='value')
    .pivot_table(index=['nr', 'col'], columns='row', values='value', aggfunc='first')
    .rename(columns={1: 'Date', 0: 'Value'})
    .reset_index()[['Date', 'Value']]
    .assign(
        Date=lambda df: pd.to_datetime(df['Date']),
        Value=lambda df: df['Value'].astype('int64')
    )
)

print(result.equals(test)) # 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

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