Omid - Challenge 352

data-challenges
advanced-exercises
🔰 Group # Group 1 Group 2 Grouping List all possible ways to divide the items into two groups with the same number of elements, and provide the solution for the IDs A, B,…
Published

March 24, 2026

Illustration for Omid - Challenge 352

Challenge Description

🔰 Group # Group 1 Group 2 Grouping List all possible ways to divide the items into two groups with the same number of elements, and provide the solution for the IDs A, B,…

Solutions

library(tidyverse)
library(readxl)

path <- "300-399/352/CH-352 Custom Grouping.xlsx"
input <- read_excel(path, range = "B3:B9") %>% pull()
test <- read_excel(path, range = "F3:H13")

result <- combn(input, 3, simplify = FALSE) %>%
  keep(~ .x[1] == input[1]) %>%
  map_dfr(
    ~ tibble(
      g1 = paste(.x, collapse = ", "),
      g2 = paste(setdiff(input, .x), collapse = ", ")
    )
  ) %>%
  select(`Group 1` = g1, `Group 2` = g2) %>%
  mutate(`Group #` = row_number(), .before = `Group 1`)

all.equal(result, test)
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • 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
from itertools import combinations

path = "300-399\\352\\CH-352 Custom Grouping.xlsx"
input = pd.read_excel(path, usecols = "B", nrows = 6, skiprows = 2 ).values.flatten().tolist()
test = pd.read_excel(path, usecols = "F:H", nrows = 10, skiprows = 2)   

df = pd.DataFrame(
    [
        ( i+1, ", ".join(c), ", ".join(sorted(set(input) - set(c))))
        for i, c in enumerate(combinations(input, 3))
        if c[0] == input[0]
    ],
    columns=["Group #", "Group 1", "Group 2"]
)

print(df.equals(test))
# True
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • 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 business rule is readable, but the workbook still requires careful implementation to reach the expected layout.