library(tidyverse)
library(readxl)
path = "files/CH-135 Identify the Pattern.xlsx"
input = read_excel(path, range = "B2:D32")
test = read_excel(path, range = "F2:G5")
count_occurences = function(string, pattern = "+-+") {
pattern_length = nchar(pattern)
chars = unlist(strsplit(string, ""))
matches = integer(0)
for (i in seq_len(length(chars) - pattern_length + 1)) {
segment = paste0(chars[i:(i + pattern_length - 1)], collapse = "")
if (segment == pattern) {
matches = c(matches, i)
}
}
return(length(matches))
}
result = input %>%
summarise(Result = str_c(Result, collapse = ""), .by = Product) %>%
mutate(`Number of repitation` = map_int(Result, count_occurences)) %>%
select(Product, `Number of repitation`) %>%
all.equal(result, test, check.attributes = FALSE)
#> [1] TRUEOmid - Challenge 135

Challenge Description
🔰 Find the number of occurrences of the “+-+” pattern across the test IDs for each product.
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
import re
path = "CH-135 Identify the Pattern.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=31)
test = pd.read_excel(path, usecols="F:G", skiprows=1, nrows=3).rename(columns=lambda x: x.split('.')[0])
def count_occurrences(string, pattern="+-+"):
return sum(1 for i in range(len(string) - len(pattern) + 1) if string[i:i + len(pattern)] == pattern)
grouped = input.groupby("Product")["Result"].agg(''.join).reset_index()
grouped['Number of repitation'] = grouped['Result'].apply(count_occurrences)
grouped.drop(columns="Result", inplace=True)
print(grouped.equals(test)) # TrueLogic:
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 to challenging:
It depends on a non-trivial iterative or rule-based transformation.
Getting the expected output requires more than one straightforward dataframe step.