library(tidyverse)
library(readxl)
path <- "300-399/381/CH-381 Filter.xlsx"
input <- read_excel(path, range = "B3:B10")
test <- read_excel(path, range = "F3:F7")
is_valid = function(num) {
digits <- str_extract_all(num, "\\d") %>% unlist() %>% as.integer()
val = sum(abs(diff(digits %% 2))) >= 2
return(val)
}
result <- input %>%
mutate(is_valid = map_lgl(ID, is_valid)) %>%
filter(is_valid)
all.equal(result$ID, test$ID)
# [1] TRUEOmid - Challenge 381
data-challenges
advanced-exercises
🔰 Challenge 381: Filter!

Challenge Description
🔰 Challenge 381: Filter!
Solutions
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 = "300-399/381/CH-381 Filter.xlsx"
input = pd.read_excel(path, usecols="B", skiprows=2, nrows=7)
test = pd.read_excel(path, usecols="F", skiprows=2, nrows=4)
def is_valid(num):
digits = [int(d) for d in re.findall(r"\d", str(num))]
parities = [d % 2 for d in digits]
diffs = [abs(parities[i + 1] - parities[i]) for i in range(len(parities) - 1)]
return sum(diffs) >= 2
result = input[input["ID"].apply(is_valid)].reset_index(drop=True)
print(result["ID"].equals(test["ID.1"]))
# [1] TRUELogic:
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.