Omid - Challenge 91

data-challenges
advanced-exercises
🔰 : Transformation!
Published

March 24, 2026

Illustration for Omid - Challenge 91

Challenge Description

🔰 : Transformation!

Solutions

library(tidyverse)
library(readxl)

path = "files/CH-091 Extract from table.xlsx"
input = read_excel(path, range = "B2:E6")
test  = read_excel(path, range = "G2:L9") %>%
  arrange(desc(Department), Name) %>%
  mutate(Name = ifelse(Name == "Mije", "Mike", Name))


result = input %>%
  pivot_longer(cols = -c(1), names_to = "Department", values_to = "Name") %>%
  mutate(Value = '✔',
         `Branch NO` = paste0("Branch ", `Branch NO`),
         Name = ifelse(Name == "Daniel", "David", Name)) %>%
  pivot_wider( names_from = "Branch NO", values_from = "Value") %>%
  arrange(desc(Department), Name) %>%
  select(Name, Department, everything())

all.equal(result, test)
# [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

path = "CH-091 Extract from table.xlsx"
input = pd.read_excel(path, usecols="B:E", skiprows= 1, nrows = 4)
test = pd.read_excel(path, usecols="G:L", skiprows=1, nrows = 8).sort_values(by=["Department", "Name"]).replace("Mije", "Mike").reset_index(drop=True)

input["Branch NO"] = "Branch " + input["Branch NO"].astype(str)
result = input.melt(id_vars="Branch NO", var_name="Department", value_name="Name")\
    .assign(Value = "✔")\
    .replace("Daniel", "David")\
    .pivot_table(index=["Department", "Name"], columns="Branch NO", values="Value", aggfunc="first")\
    .sort_values(by=["Department", "Name"]).reset_index()

result = result[["Name", "Department", "Branch 1", "Branch 2", "Branch 3", "Branch 4"]].rename_axis(None, axis=1)

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.