Omid - Challenge 199

data-challenges
advanced-exercises
🔰 Question First Name Middle Name Last Name Pattern John Paul Smith
Published

March 24, 2026

Illustration for Omid - Challenge 199

Challenge Description

🔰 Question First Name Middle Name Last Name Pattern John Paul Smith

Solutions

library(tidyverse)
library(readxl)

path = "files/CH-199 Combining the columns.xlsx"
input = read_excel(path, range = "B2:E7")
test  = read_excel(path, range = "H2:H7")

result = input %>%
  mutate(result = pmap_chr(
    list(`First Name`, `Middle Name`, `Last Name`, Pattern),
    function(a, b, c, ord_str) {
      order <- as.integer(str_split(ord_str, ",", simplify = TRUE))
      paste(c(a, b, c)[order], collapse = " ")
    }
  )) %>%
  select(result)

all.equal(result, test, check.attributes = FALSE)
#> [1] TRUE
  • 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

path = "CH-199 Combining the columns.xlsx"
input = pd.read_excel(path, usecols="B:E", skiprows=1, nrows=6)
test = pd.read_excel(path, usecols="H", skiprows=1, nrows=6)

def combine_columns(row):
    first_name, middle_name, last_name, pattern = row
    order = list(map(int, pattern.split(',')))
    names = [first_name, middle_name, last_name]
    return ' '.join([names[i-1] for i in order])

input['Custom Format'] = input.apply(combine_columns, axis=1)
result = input['Custom Format']

print(all(input['Custom Format'] == test['Custom Format'])) # True
  • 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 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.