library(tidyverse)
library(readxl)
path = "files/200-299/236/CH-236 Column Splitting.xlsx"
input = read_excel(path, range = "B2:B7")
test = read_excel(path, range = "D2:F7")
test[is.na(test)] <- ""
split_id <- function(s) {
if (str_detect(s, "^[A-Za-z]{3}")) {
list(
str_sub(s, 1, 3),
str_sub(s, 4, 6),
str_sub(s, 7, str_length(s))
)
} else {
list(s, "", "")
}
}
result = input %>%
mutate(`ID.` = map(ID, split_id)) %>%
unnest_wider(`ID.`, names_sep = "") %>%
select(-c(1))
all.equal(result, test)
# [1] TRUEOmid - Challenge 236
data-challenges
advanced-exercises
🔰 Question Result ID ID.1 ID.2 ID.3 ABC123 ABC

Challenge Description
🔰 Question Result ID ID.1 ID.2 ID.3 ABC123 ABC
Solutions
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/236/CH-236 Column Splitting.xlsx"
input_df = pd.read_excel(path, usecols="B", skiprows=1, nrows=6)
test_df = pd.read_excel(path, usecols="D:F", skiprows=1, nrows=6).fillna("")
def split_id(s):
s = str(s)
if re.match(r"^[A-Za-z]{3}", s):
return [s[:3], s[3:6], s[6:]]
else:
return [s, "", ""]
result = input_df['ID'].apply(split_id).apply(pd.Series)
result.columns = ['ID.1', 'ID.2', 'ID.3']
print(result.equals(test_df)) # TrueLogic:
Reads the workbook ranges needed for the challenge
Parses the text patterns directly instead of relying on manual cleanup
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.