library(tidyverse)
library(readxl)
path = "files/CH-162 Extract from Text.xlsx"
input = read_excel(path, range = "B2:C7")
test = read_excel(path, range = "E2:I7")
r1 = input %>%
mutate(Value = str_replace_all(Value, "(\\d),\\{", "\\1},\\{")) %>%
mutate(Value = str_replace_all(Value, "\\{+", "{")) %>%
mutate(Value = str_replace_all(Value, "\\}+", "}")) %>%
separate_rows(Value, sep = "(?<=\\}),(?=\\{)") %>%
mutate(rn = row_number(), .by = ID) %>%
pivot_wider(names_from = rn, values_from = Value) %>%
setNames(colnames(test))
all.equal(r1, test, check.attributes = FALSE)
# TRUEOmid - Challenge 162

Challenge Description
🔰 Challenge 162: Extract From Text
Solutions
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
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 = "CH-162 Extract from Text.xlsx"
input = pd.read_excel(path, usecols="B:C", skiprows=1, nrows=6)
test = pd.read_excel(path, usecols="E:I", skiprows=1, nrows=6)
def clean_value(value):
value = re.sub(r"(\d),\{", r"\1},{", value)
value = re.sub(r"\{+", "{", value)
value = re.sub(r"\}+", "}", value)
return value
input['Value'] = input['Value'].apply(clean_value)
input = input.assign(Value=input['Value'].str.split(r"(?<=\}),(?=\{)")).explode('Value')
input['rn'] = input.groupby('ID').cumcount() + 1
r1 = input.pivot(index='ID', columns='rn', values='Value').reset_index()
r1.columns = test.columns
print(r1.equals(test)) # TrueLogic:
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 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.