library(tidyverse)
library(readxl)
path = "Excel/700-799/711/711 Generate a Sequence.xlsx"
input = read_excel(path, range = "A1:A6")
test = read_excel(path, range = "A1:K6")
extract_sequence <- function(start_num) {
pad_and_extract <- function(n) {
sq <- n^2
sq_str <- ifelse(nchar(sq) < 8, sprintf("%08d", sq), as.character(sq))
as.integer(substr(sq_str, 3, 6))
}
sequence <- start_num
repeat {
next_val <- pad_and_extract(tail(sequence, 1))
if (next_val %in% sequence) break
sequence <- c(sequence, next_val)
}
return(sequence)
}
result = input %>%
mutate(seq = map(Number, ~ extract_sequence(.x))) %>%
unnest(seq) %>%
group_by(Number) %>%
slice(-1) %>%
filter(seq != 0) %>%
mutate(rn = row_number()) %>%
filter(rn <= 10) %>%
ungroup() %>%
pivot_wider(names_from = rn, values_from = seq)
result == testExcel BI - Excel Challenge 711
excel-challenges
excel-formulas
🔰 3.

Challenge Description
🔰 3. Starting with 3rd digit, extract 4 digits number from step 1. 3. Starting with 3rd digit, extract 4 digits number from step 1.
Solutions
- Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Aggregate or rank the data at the required grouping level; Reshape the result into the workbook output format.
- Strengths: The transformation is organized around the correct grouping level, which keeps the business logic clear.
- Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
- Gem: The key move is solving the problem at the right grain before shaping the final output.
import pandas as pd
path = "700-799/711/711 Generate a Sequence.xlsx"
input = pd.read_excel(path, usecols="A", nrows=6)
test = pd.read_excel(path, usecols="A:K", nrows=6)
def extract_sequence(start_num):
sequence, pad_and_extract = [start_num], lambda n: int(f"{n**2:08d}"[2:6])
while (next_val := pad_and_extract(sequence[-1])) not in sequence:
sequence.append(next_val)
return sequence
result = (
input
.assign(seq=input.iloc[:, 0].map(lambda x: extract_sequence(x)))
.explode('seq')
.groupby(input.columns[0])
.apply(lambda group: group.iloc[1:])
.reset_index(drop=True)
.query("seq != 0")
.assign(rn=lambda df: df.groupby(input.columns[0]).cumcount() + 1)
.query("rn <= 10")
.pivot(index=input.columns[0], columns='rn', values='seq')
.reset_index()
)
print(result)The Python version follows the same grouped logic and keeps the transformation explicit in a dataframe pipeline.
Difficulty Level
Medium
The individual steps are manageable, but the correct transformation pattern is not obvious from the raw data.