library(tidyverse)
library(readxl)
path = "files/200-299/245/CH-245 Custom Ranking.xlsx"
input = read_excel(path, range = "B2:D17")
test = read_excel(path, range = "F2:I17")
result = input %>%
mutate(
group_total = sum(Score),
group_size = n(),
in_group_rank = rank(-Score, ties.method = "first"),
.by = Group
) %>%
mutate(
group_rank = dense_rank(-group_total),
Rank = in_group_rank + (group_rank - 1) * group_size
) %>%
select(ID, Group, Score, Rank) %>%
arrange(Rank)
all.equal(result, test, check.attributes = FALSE)
#> [1] TRUEOmid - Challenge 245
data-challenges
advanced-exercises
🔰 Group The Question table shows the participants of a competition divided into three different groups, sorted by their individual scores.

Challenge Description
🔰 Group The Question table shows the participants of a competition divided into three different groups, sorted by their individual scores.
Solutions
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
path = "200-299/245/CH-245 Custom Ranking.xlsx"
input = pd.read_excel(path, usecols="B:D", skiprows=1, nrows=15)
test = pd.read_excel(path, usecols="F:I", skiprows=1, nrows=15).rename(columns=lambda col: col.replace('.1', ''))
g = input.groupby('Group', group_keys=False)
input['group_total'] = g['Score'].transform('sum')
input['group_size'] = g['Score'].transform('size')
input['in_group_rank'] = g['Score'].rank(ascending=False, method='first')
group_totals = input.groupby('Group', as_index=False)['group_total'].first()
group_totals['group_rank'] = group_totals['group_total'].rank(ascending=False, method='first').astype(int)
input = input.merge(group_totals[['Group', 'group_rank']], on='Group')
input['Rank'] = (input['in_group_rank'] + (input['group_rank'] - 1) * input['group_size']).astype(int)
result = input[['ID', 'Group', 'Score', 'Rank']].sort_values('Rank').reset_index(drop=True)
print(result.equals(test)) # TrueLogic:
Reads the workbook ranges needed for the challenge
Aggregates or ranks values at the relevant grouping level
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.