Omid - Challenge 255

data-challenges
advanced-exercises
🔰 Convert embedded pseudo-HTML data with optional fields and nested tags into a structured table.
Published

March 24, 2026

Illustration for Omid - Challenge 255

Challenge Description

🔰 Convert embedded pseudo-HTML data with optional fields and nested tags into a structured table.

Solutions

library(tidyverse)
library(readxl)

path = "files/200-299/255/CH-255 Parse HTML.xlsx"
input = read_excel(path, range = "B2:B7")
test  = read_excel(path, range = "D2:G7")

parse_markup = function(text) {
  str_match_all(text, "<([^>]+)>([^<]+)</\\1>")[[1]] %>% 
    as_tibble(.name_repair = "unique") %>% 
    select(tag = 2, value = 3) %>%
    pivot_wider(names_from = tag, values_from = value) 
}

result = input %>%
  mutate(parsed = map(`Raw Text`, parse_markup)) %>%
  unnest(parsed) %>%
  select(-`Raw Text`) %>%
  set_names(names(test)) %>%
  mutate(across(c(ID, Value), as.numeric))

all.equal(result, test, check.attributes = FALSE) 
# TRUE
  • 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 = "200-299/255/CH-255 Parse HTML.xlsx"
input = pd.read_excel(path, usecols="B", skiprows=1, nrows=6)
test = pd.read_excel(path, usecols="D:G", skiprows=1, nrows=6)

def parse_markup(text):
    matches = re.findall(r"<([^>]+)>([^<]+)</\1>", str(text))
    return {tag: value for tag, value in matches}

parsed = input["Raw Text"].apply(parse_markup).apply(pd.Series)
result = pd.concat([input, parsed], axis=1).drop(columns=["Raw Text"])
result.columns = test.columns
result[["ID", "Value"]] = result[["ID", "Value"]].apply(pd.to_numeric, errors="coerce")

print(result.equals(test))
  • 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.