Omid - Challenge 97

data-challenges
advanced-exercises
🔰 Question Result A B C Year Challenge 97: Linear Interpolation!
Published

March 24, 2026

Illustration for Omid - Challenge 97

Challenge Description

🔰 Question Result A B C Year Challenge 97: Linear Interpolation!

Solutions

library(tidyverse)
library(readxl)
library(zoo)

path = "files/CH-097 Linear Interpolation.xlsx"
input = read_excel(path, range = "B2:E5")
test = read_excel(path, range = "H2:K15")

years = tibble(Year = 2010:2022)

df = years %>%
  left_join(input, by = c("Year" = "Year")) %>%
  mutate(across(c("A", "B", "C"), ~ na.approx(.x, na.rm = FALSE))) %>%
  mutate(across(
    c("A", "B", "C"),
    ~ case_when(
      row_number() == 1 ~ 2 * lead(.x) - lead(lead(.x)),
      row_number() == n() ~ 2 * lag(.x) - lag(lag(.x)),
      TRUE ~ .x
    )
  )) %>%
  mutate(across(c("A", "B", "C"), round, 0))

identical(df, test) # TRUE
  • Logic:

    • Reads the workbook ranges needed for the challenge

    • Builds the intermediate columns that drive the final result

  • 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 numpy as np

path = 'CH-097 Linear Interpolation.xlsx'
input = pd.read_excel(path, usecols = "B:E", skiprows = 1, nrows = 3)
test  = pd.read_excel(path, usecols = "H:K", skiprows = 1, nrows = 13) 
test.columns = ['Year', 'A', 'B', 'C']

years = pd.DataFrame({'Year': range(2010, 2023)})
r1 = years.merge(input, how='left', on='Year')

r1[['A', 'B', 'C']] = r1[['A', 'B', 'C']].interpolate().round().astype(int)

r1.iloc[[0, -1], 1:] = 2 * r1.iloc[[1, -2], 1:] - r1.iloc[[2, -3], 1:]
print(r1.equals(test)) # True
  • Logic:

    • Reads the workbook ranges needed for the challenge
  • 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 business rule is readable, but the workbook still requires careful implementation to reach the expected layout.