library(tidyverse)
library(readxl)
input = read_excel("files/CH-043 Sort Table columns .xlsx", range = "B2:G9")
test = read_excel("files/CH-043 Sort Table columns .xlsx", range = "J2:O9")
result = input %>% select(Regions, names(sort(colSums(select(., -Regions)), decreasing = TRUE)))
identical(result, test)
# [1] TRUEOmid - Challenge 43
data-challenges
advanced-exercises
🔰 Product A Product B Product C Question Result Region 1 Region 2 Region 3

Challenge Description
🔰 Product A Product B Product C Question Result Region 1 Region 2 Region 3
Solutions
Logic:
- Reads the workbook ranges needed for the challenge
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 re
input = pd.read_excel('CH-043 Sort Table columns .xlsx', usecols="B:G", skiprows=1, nrows = 7)
test = pd.read_excel('CH-043 Sort Table columns .xlsx', usecols="J:O", skiprows=1, nrows = 7)
test.columns = test.columns.str.replace(r'\.1$', '', regex=True)
result = input[['Regions'] + input.drop('Regions', axis=1).sum().sort_values(ascending=False).index.tolist()]
print(result.equals(test)) # TrueLogic:
Reads the workbook ranges needed for the challenge
Parses the text patterns directly instead of relying on manual cleanup
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.