Omid - Challenge 244

data-challenges
advanced-exercises
🔰 Question Result ID MN-B-10-12 MN-123 MN-B 10-12 ID.1
Published

March 24, 2026

Illustration for Omid - Challenge 244

Challenge Description

🔰 Question Result ID MN-B-10-12 MN-123 MN-B 10-12 ID.1

Solutions

library(tidyverse)
library(readxl)

path = "files/200-299/244/CH-244 Column Splitting.xlsx"
input = read_excel(path, range = "B2:B7")
test = read_excel(path, range = "D2:E7")

split_by_sec_del = function(text) {
  hyph_loc = str_locate_all(text, "-")[[1]]
  sl_loc = str_locate_all(text, "\\/")[[1]]

  if (nrow(hyph_loc) < 2 & nrow(sl_loc) < 2) {
    return(text)
  } else if (nrow(hyph_loc) >= 2 & nrow(sl_loc) >= 2) {
    split_loc <- min(hyph_loc[2, 1], sl_loc[2, 1])
  } else if (nrow(hyph_loc) >= 2) {
    split_loc <- hyph_loc[2, 1]
  } else {
    split_loc <- sl_loc[2, 1]
  }
  first_part = str_sub(text, 1, split_loc - 1)
  second_part = str_sub(text, split_loc + 1)
  return(c(first_part, second_part))
}

result = input %>%
  mutate(ID = map(ID, split_by_sec_del)) %>%
  unnest_wider(ID, names_sep = ".")

all.equal(result, test, check.attributes = FALSE)
# TRUE
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • 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
import re

path = "200-299/244/CH-244 Column Splitting.xlsx"
input = pd.read_excel(path, usecols="B", skiprows=1, nrows=6)
test = pd.read_excel(path, usecols="D:E", skiprows=1, nrows=6)

def split_by_sec_del(text):
    if pd.isna(text): return [text, None]
    h = [m.start() for m in re.finditer("-", str(text))]
    s = [m.start() for m in re.finditer("/", str(text))]
    if sum(len(x) >= 2 for x in [h, s]) == 0: return [text, None]
    if h and s and len(h) >= 2 and len(s) >= 2:
        i = min(h[1], s[1])
    elif len(h) >= 2:
        i = h[1]
    else:
        i = s[1]
    return [text[:i], text[i+1:]]

split_cols = input.iloc[:, 0].apply(split_by_sec_del)
result = pd.DataFrame(split_cols.tolist(), columns=["ID.1", "ID.2"])

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

    • Reads the workbook ranges needed for the challenge

    • Parses the text patterns directly instead of relying on manual cleanup

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