Forum Discussion
PeterBartholomew1
Jan 18, 2025Silver Contributor
What do you think of thunks?
OK, so the most likely response by far is going to be "I don't". However, I tried one of Omid Motamedisedeh's regular challenges and found it a suitable problem for exploring some of the lesser k...
Patrick2788
Jan 20, 2025Silver Contributor
This is a fun challenge because there's several ways to solve this one. ROLLINGRANGEλ is subtle and efficient! Took me a few minutes to see where you hid the thunking. One might argue this is a "black box" but this isn't the type of function most would be tinkering with to get into trouble. I tested the rolling max at 50,000 and 100,000 elements and the calc time was very fast.
My solutions are going to seem pedestrian in comparison:
=LET(
n, 2,
i, SEQUENCE(ROWS(dataValues)),
j, SEQUENCE(, n ^ 2 + 1, -n, 1),
M, INDEX(dataValues, i + j),
keep, BYROW(
M,
LAMBDA(each_row,
LET(val, INDEX(each_row, , n + 1), max_val, MAX(TOROW(each_row, 2)), val = max_val)
)
),
FILTER(Data, keep)
)
My novice solution with pandas:
#Set interval and day offset.
n = xl("interval") ** 2 + 1
day_offset = f"{n}D" #Converts to '5D'
#Set table as data frame and set 'Date' column as index.
df = pd.DataFrame(xl("Data[#All]", headers=True))
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# Calculate rolling max for 'n' days before and after each day (designed as 'center').
rolling_max = df.rolling(day_offset, min_periods=1, center=True).max()
rolling_max