Omid - Challenge 94

data-challenges
advanced-exercises
🔰 Group In the question table, texts are provided for different groups.
Published

March 24, 2026

Illustration for Omid - Challenge 94

Challenge Description

🔰 Group In the question table, texts are provided for different groups.

Solutions

library(tidyverse)
library(readxl)

path = "files/CH-094 Two Column Text.xlsx"
input = read_excel(path, range = "B2:C13")
test  = read_excel(path, range = "H2:J9")

result = input %>%
  mutate(column = ifelse(row_number() %% 2 == 0, 2, 1),
         row = ceiling(row_number() / 2), 
         .by = Group) %>%
  pivot_wider(names_from = column, values_from = Text, names_glue = "Column {column}") %>%
  select(-row)

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-094 Two Column Text.xlsx"
input = pd.read_excel(path, skiprows=1, usecols = "B:C")
test  = pd.read_excel(path, skiprows=1, usecols = "H:J", nrows = 7)
test.columns = test.columns.str.replace(".1", "")

result = input.copy()
result["RowNumber"] = result.groupby("Group").cumcount() + 1
result["col"] = result["RowNumber"].apply(lambda x: 2 if x % 2 == 0 else 1)
result["row"] = result["RowNumber"].apply(lambda x: (x + 1) // 2)

result = result.pivot(index = ["Group", "row"], columns = "col", values = "Text")
result.columns = [f"Column {col}" for col in result.columns]
result = result.reset_index()
result = result.drop(columns = "row")

print(result.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

    • Applies the rule iteratively until the output stabilizes

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