library(tidyverse)
library(readxl)
input = read_excel("files/CH-009.xlsx", range = "B2:D32")
test1 = read_excel("files/CH-009.xlsx", range = "F2:G5") %>% janitor::clean_names()
test2 = read_excel("files/CH-009.xlsx", range = "I2:J5") %>% janitor::clean_names()
result = input %>%
group_by(Product) %>%
summarise(seq = str_c(Result, collapse = "")) %>%
ungroup()
# pattern +--
process_pattern1 = function(string) {
string = string %>%
str_replace_all("\\+\\-\\-", "3") %>%
str_replace_all("\\-\\-3", "/5") %>%
str_replace_all("3\\+\\-", "5/") %>%
str_replace_all("\\-3", "/4") %>%
str_replace_all("3\\+", "4/") %>%
str_replace_all("/4\\+", "/5/") %>%
str_replace_all("44", "8") %>%
str_replace_all("35", "8")
numbers = as.numeric(str_extract_all(string, "")[[1]]) %>% max(., na.rm = TRUE)
return(numbers)
}
result1 = result %>%
rowwise() %>%
mutate(max_seq = map_dbl(seq, process_pattern1))
identical(result1$max_seq, test1$x)
#> [1] TRUE
# pattern +-
process_pattern2 = function(string) {
string = string %>%
str_replace_all("\\+\\-", "2") %>%
str_replace_all("^\\-2", "3") %>%
str_replace_all("2\\+$", "3") %>%
str_replace_all("\\-3", "4") %>%
str_replace_all("22", "4") %>%
str_replace_all("32|23", "5") %>%
str_replace_all("44", "8")
numbers = as.numeric(str_extract_all(string, "")[[1]]) %>% max(., na.rm = TRUE)
return(numbers)
}
result2 = result %>%
rowwise() %>%
mutate(max_seq = map_dbl(seq, process_pattern2))
identical(result2$max_seq, test2$x)
#> [1] TRUEOmid - Challenge 9

Challenge Description
🔰 Find the length of Largest repetition of a specific pattern!
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
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
input_data = pd.read_excel("CH-009.xlsx", usecols="B:D", skiprows=1, nrows=31)
test1 = pd.read_excel("CH-009.xlsx", usecols="F:G", skiprows=1, nrows=4)
test2 = pd.read_excel("CH-009.xlsx", usecols="I:J", skiprows=1, nrows=4)
result = input_data.groupby("Product", as_index=False)["Result"].agg(seq="".join)
def process_pattern1(string):
string = (string.replace("+--", "3")
.replace("--3", "/5")
.replace("3+-", "5/")
.replace("-3", "/4")
.replace("3+", "4/")
.replace("/4+", "/5/")
.replace("44", "8")
.replace("35", "8"))
digits = [int(ch) for ch in string if ch.isdigit()]
return max(digits) if digits else 0
def process_pattern2(string):
string = (string.replace("+-", "2")
.replace("-2", "3", 1) if string.startswith("-2") else string)
if string.endswith("2+"):
string = string[:-2] + "3"
string = (string.replace("-3", "4")
.replace("22", "4")
.replace("32", "5")
.replace("23", "5")
.replace("44", "8"))
digits = [int(ch) for ch in string if ch.isdigit()]
return max(digits) if digits else 0
result1 = result.assign(max_seq=result["seq"].map(process_pattern1))
result2 = result.assign(max_seq=result["seq"].map(process_pattern2))
print(result1["max_seq"].equals(test1.iloc[:, 1]))
print(result2["max_seq"].equals(test2.iloc[:, 1]))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
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.