Omid - Challenge 275

data-challenges
advanced-exercises
🔰 : Text Matching!
Published

March 24, 2026

Illustration for Omid - Challenge 275

Challenge Description

🔰 : Text Matching!

Solutions

library(tidyverse)
library(readxl)
library(charcuterie)

path = "files/200-299/275/CH-275 Text Matching.xlsx"
input = read_excel(path, range = "B2:B9")
test  = read_excel(path, range = "D2:E8")

r1 = input %>%
  mutate(ID_clean = str_remove_all(ID, "[^[:alnum:]]"),
         chars_id = map(ID_clean, ~ unique(chars(.x))))

r2 = expand.grid(i = seq_len(nrow(r1)), j = seq_len(nrow(r1))) %>%
  filter(i < j) %>%
  mutate(
    inter = map2(r1$chars_id[i], r1$chars_id[j], ~ intersect(sort(.x), sort(.y)))
  ) %>%
  filter(map_int(inter, length) >= 3) %>%
  transmute(`ID 1` = r1$ID[j], `ID 2` = r1$ID[i])

# the same pairs in different order
  • 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 = "200-299/275/CH-275 Text Matching.xlsx"
input = pd.read_excel(path, usecols="B", skiprows=1, nrows=7)

input['chars_id'] = input.iloc[:,0].apply(lambda s: set(re.sub(r'[^A-Za-z0-9]', '', str(s))))

pairs = [
    {'ID 1': input.iloc[j, 0], 'ID 2': input.iloc[i, 0]}
    for i in range(len(input))
    for j in range(i+1, len(input))
    if len(input.at[i, 'chars_id'] & input.at[j, 'chars_id']) >= 3
]

result = pd.DataFrame(pairs)
print(result) # Correct pais in different order
  • 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.