Omid - Challenge 130

data-challenges
advanced-exercises
🔰 Question Result Input Output Quantity Date Tyoe Output A
Published

March 24, 2026

Illustration for Omid - Challenge 130

Challenge Description

🔰 Question Result Input Output Quantity Date Tyoe Output A

Solutions

library(tidyverse)
library(readxl)

path = "files/CH-130 FIFO.xlsx"
input = read_excel(path, range = "B2:D13")
test  = read_excel(path, range = "F2:I11")

i_data = input %>%
  filter(str_starts(Tyoe, "I")) %>%
  uncount(Quantity, .remove = F) %>%
  mutate(In = 1, rn = row_number())

o_data = input %>%
  filter(str_starts(Tyoe, "O")) %>%
  uncount(Quantity, .remove = F) %>%
  mutate(Out = 1, rn = row_number())

all = full_join(i_data, o_data, by = "rn") %>%
  summarise(all = sum(In, na.rm = T), .by = c(Date.x, Date.y, Tyoe.x, Tyoe.y)) %>%
  na.omit() %>%
  mutate(Output = str_sub(Tyoe.y, -1, -1)) %>%
  select(Output, `Registered Date` = Date.y, `Source Date` = Date.x, Quantity = all)

all.equal(all, test, check.attributes = F)
#> [1] TRUE
  • 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

path = "CH-130 FIFO.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=11)
test = pd.read_excel(path, usecols="F:I", skiprows=1, nrows=9).rename(columns=lambda x: x.replace('.1', ''))

i_data = input[input['Tyoe'].str.startswith('I')].copy()
i_data = i_data.loc[i_data.index.repeat(i_data['Quantity'])].assign(In=1, rn=lambda x: range(1, len(x) + 1)).reset_index(drop=True)

o_data = input[input['Tyoe'].str.startswith('O')].copy()
o_data = o_data.loc[o_data.index.repeat(o_data['Quantity'])].assign(Out=1, rn=lambda x: range(1, len(x) + 1)).reset_index(drop=True)

all_data = pd.merge(i_data, o_data, on='rn', how='outer').dropna().sort_values(by='rn').reset_index(drop=True)
all_data = all_data.groupby(['Date_x', 'Date_y', 'Tyoe_x', 'Tyoe_y'], as_index=False)['In'].sum()
all_data["Output"] = all_data['Tyoe_y'].str[-1]

all_data = all_data.drop(columns=['Tyoe_x', 'Tyoe_y']).reindex(columns=['Output', 'Date_y', 'Date_x', 'In'])
all_data.columns = test.columns

print(all_data.equals(test)) # True
  • 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

  • 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.