library(tidyverse)
library(readxl)
path = "files/CH-185 Replace consecutive X.xlsx"
input = read_excel(path, range = "C2:D10")
test = read_excel(path, range = "F2:G10")
result = input %>%
mutate(ID = str_replace_all(ID, "[Xx]+", "X"))
all.equal(result, test)
#> [1] TRUEOmid - Challenge 185
data-challenges
advanced-exercises
🔰 Result Question Date ID XM-XX12 IXXXM3X 87X X8X9X1

Challenge Description
🔰 Result Question Date ID XM-XX12 IXXXM3X 87X X8X9X1
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-185 Replace consecutive X.xlsx"
input = pd.read_excel(path,usecols="C:D", skiprows=1, nrows=9)
test = pd.read_excel(path, usecols="F:G", skiprows=1, nrows=9).rename(columns=lambda x: x.split('.')[0])
input['ID'] = [re.sub(r'[Xx]+', 'X', str(x)) for x in input['ID']]
print(input.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.