Omid - Challenge 331

data-challenges
advanced-exercises
🔰 Question Table Result Table Column 1 Column 2 A X C B
Published

March 24, 2026

Illustration for Omid - Challenge 331

Challenge Description

🔰 Question Table Result Table Column 1 Column 2 A X C B

Solutions

library(tidyverse)
library(readxl)

path <- "300-399/331/CH-331 Pattern Combinations.xlsx"
input <- read_excel(path, range = "B2:C9")
test <- read_excel(path, range = "E2:E4") %>% pull()

result <- input %>%
  mutate(
    p1 = ifelse(row_number() %% 2 == 1, `Column 1`, `Column 2`),
    p2 = ifelse(row_number() %% 2 == 1, `Column 2`, `Column 1`)
  ) %>%
  select(p1, p2) %>%
  summarise(across(everything(), ~ paste(., collapse = ""))) %>%
  pivot_longer(everything(), values_to = "pattern") %>%
  pull(pattern)

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

    • Aggregates or ranks values at the relevant grouping level

    • 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 = "300-399/331/CH-331 Pattern Combinations.xlsx"
input = pd.read_excel(path, usecols="B:C", skiprows=1, nrows=8)
test = pd.read_excel(path, usecols="E", skiprows=1, nrows=2).iloc[:, 0].tolist()

p1_str = "".join(np.where(input.index % 2 == 0, input.iloc[:, 0], input.iloc[:, 1]).astype(str))
p2_str = "".join(np.where(input.index % 2 == 0, input.iloc[:, 1], input.iloc[:, 0]).astype(str))

result = [p1_str, p2_str]
print(result == test) # True
  • Logic:

    • Reads the workbook ranges needed for the challenge
  • 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.