library(tidyverse)
library(readxl)
path = "files/CH-164 Extract from Text.xlsx"
input = read_excel(path, range = "B2:C7")
test = read_excel(path, range = "E2:F7")
maxDepth = function(S) {
get_chars = function(str) {
tibble(pos = 1:nchar(str), char = strsplit(str, "")[[1]])
}
check = if_else(str_detect(S, "^[{]+[0-9,]+[}]+$"), -1, 0)
df = get_chars(S)
df = df %>%
mutate(count = ifelse(char == "{", 1, ifelse(char == "}", -1, 0)),
cum_sum = cumsum(count))
max_depth = max(df$cum_sum) + check
return(max_depth)
}
result = input %>%
mutate(Depth = map_dbl(Value, maxDepth))
all.equal(result$Depth, test$Depth)
#> [1] TRUEOmid - Challenge 164
data-challenges
advanced-exercises
🔰 Challenge 164: Extract From Text In Power Query, a list is defined by { } and can contain sublists, such as {1, 2, {3, 4}}.

Challenge Description
🔰 Challenge 164: Extract From Text In Power Query, a list is defined by { } and can contain sublists, such as {1, 2, {3, 4}}.
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 = "CH-164 Extract from Text.xlsx"
input = pd.read_excel(path, usecols="B:C", skiprows=1, nrows=6)
test = pd.read_excel(path, usecols="E:F", skiprows=1, nrows=6)
def max_depth(S):
get_chars = lambda string: pd.DataFrame({'pos': range(1, len(string) + 1), 'char': list(string)})
check = -1 if re.match(r'^[{]+[0-9,]+[}]+$', S) else 0
df = get_chars(S)
df['cum_sum'] = df['char'].apply(lambda x: 1 if x == '{' else (-1 if x == '}' else 0)).cumsum()
max_depth = df['cum_sum'].max() + check
return max_depth
input['depth'] = input['Value'].apply(max_depth)
print(input['depth'].equals(test['Depth'])) # 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.