library(tidyverse)
library(readxl)
path = "CH-189 Combining the columns.xlsx"
input = read_excel(path, range = "B2:E7")
test = read_excel(path, range = "H2:H7")
result = input %>%
mutate(Pattern = trimws(Pattern)) %>%
separate_rows(Pattern, sep = "") %>%
mutate(repl = case_when(
Pattern == "F" ~ `First Name`,
Pattern == "L" ~ `Last Name`,
Pattern == "M" ~ `Middle Name`,
TRUE ~ Pattern
)) %>%
summarise(`Custom Format` = paste(repl, collapse = ""), .by = `First Name`)Omid - Challenge 189
data-challenges
advanced-exercises
🔰 Question First Name Middle Name Last Name Pattern John Paul Smith

Challenge Description
🔰 Question First Name Middle Name Last Name Pattern John Paul Smith
Solutions
Logic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
Builds the intermediate columns that drive the final result
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-189 Combining the columns.xlsx"
input = pd.read_excel(path, usecols="B:E", skiprows=1, nrows=6)
def replace_pattern(row):
return ''.join(row['First Name'] if c == 'F' else row['Last Name'] if c == 'L' else row['Middle Name'] if c == 'M' else c for c in row['Pattern'].strip())
input['Custom Format'] = input.apply(replace_pattern, axis=1)
result = input.groupby('First Name')['Custom Format'].apply(''.join).reset_index()
print(result)Logic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
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.