Omid - Challenge 59

data-challenges
advanced-exercises
🔰 The power consumption of a house every 30 minutes on different dates is provided in the question table.
Published

March 24, 2026

Illustration for Omid - Challenge 59

Challenge Description

🔰 The power consumption of a house every 30 minutes on different dates is provided in the question table.

Solutions

library(tidyverse)
library(readxl)

input = read_excel("files/CH-059 Merge Columns.xlsx", range = "B2:J12")
test  = read_excel("files/CH-059 Merge Columns.xlsx", range = "L2:P12")

result = input %>%
  pivot_longer(cols = -c(1), names_to = "time", values_to = "value") %>%
  mutate(time = str_sub(time, 1,4) %>% paste0(., "00")) %>%
  pivot_wider(names_from = time, values_from = value, values_fn = sum) 

identical(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

    • 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

input = pd.read_excel("CH-059 Merge Columns.xlsx", usecols="B:J", skiprows=1, nrows = 10)
test = pd.read_excel("CH-059 Merge Columns.xlsx", usecols="L:P", skiprows=1, nrows = 10)
test.columns = test.columns.str.replace(".1", "")

result = input.melt(id_vars=["Date"], var_name="time", value_name="value")
result["time"] = result["time"].str[:4] + "00"
result = result.pivot_table(index="Date", columns="time", values="value", aggfunc="sum").reset_index()

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.