library(tidyverse)
library(readxl)
library(stringi)
path <- "300-399/368/CH-368 Text Cleaning.xlsx"
input <- read_excel(path, range = "B3:B8")
test <- read_excel(path, range = "E3:E8")
mirror_half <- function(x) {
n <- nchar(x)
best <- ""
for (i in seq_len(n)) {
for (len in seq_len((n - i + 1) %/% 2)) {
left <- substr(x, i, i + len - 1)
right <- substr(x, i + len, i + 2 * len - 1)
if (left == stri_reverse(right) && len > nchar(best)) {
best <- left
}
}
}
best
}
result = input %>%
mutate(ID = map_chr(ID, mirror_half))
all.equal(result$ID, test$ID)
# Last one correct but not accordign to given answers.Omid - Challenge 368
data-challenges
advanced-exercises
🔰 In the id column in the question table, find

Challenge Description
🔰 In the id column in the question table, find
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Builds the intermediate columns that drive the final result
Applies the rule iteratively until the output stabilizes
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
path = "300-399/368/CH-368 Text Cleaning.xlsx"
input = pd.read_excel(path, usecols="B", nrows = 6, skiprows = 2)
test = pd.read_excel(path, usecols="E", nrows = 6, skiprows =2 )
def mirror_half(s):
n = len(s)
best = ""
for i in range(n):
for l in range(1, (n - i + 1) // 2 + 1):
left = s[i:i+l]
right = s[i+l:i+2*l]
if left == right[::-1] and len(left) > len(best):
best = left
return best
input["Cleaned"] = input["ID"].apply(mirror_half)
print(input["Cleaned"].equals(test["ID.1"]))
# Last one correct but not according to the expected output.Logic:
Reads the workbook ranges needed for the challenge
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 business rule is readable, but the workbook still requires careful implementation to reach the expected layout.