Challenge

Problem

Business analytics often requires aggregating time-series data into meaningful periods for reporting and decision-making. This drill teaches you to parse dates, group transactions by month, calculate aggregate statistics, and format currency. You'll learn date manipulation, hash-based grouping, aggregation functions, and business reporting—essential skills for building dashboards, analytics tools, and financial reports.

Difficulty: Intermediate

Instructions

  1. Read sales transactions from sales.csv (Date, Product, Amount)
  2. Parse dates and group transactions by month (YYYY-MM format)
  3. For each month, calculate:
    • Total sales amount
    • Number of transactions
    • Average transaction value
  4. Sort months chronologically
  5. Print monthly report:
    'MONTHLY SALES REPORT'
    '=' * 50
    'Month: YYYY-MM'
    ' Transactions: X'
    ' Total Sales: $X,XXX.XX'
    ' Average: $XXX.XX'
    (blank line between months)
  6. Print summary:
    '-' * 50
    'OVERALL SUMMARY'
    'Total Transactions: X'
    'Total Sales: $X,XXX.XX'
    'Average per Transaction: $XXX.XX'

Files

Editable
Read-only

Hints

Hint 1

Date.parse(string) converts date string to Date object

Hint 2

date.strftime('%Y-%m') formats date as 'YYYY-MM'

Hint 3

Hash.new { |h, k| h[k] = [] } creates hash with array default values

Hint 4

array.sum calculates total of all elements

Hint 5

hash.keys.sort sorts month strings alphabetically (which is chronological)

Hint 6

hash.values.flatten combines all arrays into one

Hint 7

Format decimals: '%.2f' % number

Hint 8

Add commas: reverse string, insert commas every 3 digits, reverse back

Provided Files (Read-only)

1. Three months of sales

Input:
analyze_sales('sales.csv')
Expected Output:
MONTHLY SALES REPORT
==================================================
Month: 2024-01
  Transactions: 3
  Total Sales: $1,419.97
  Average: $473.32

Month: 2024-02
  Transactions: 3
  Total Sales: $1,779.97
  Average: $593.32

Month: 2024-03
  Transactions: 4
  Total Sales: $1,869.96
  Average: $467.49

--------------------------------------------------
OVERALL SUMMARY
Total Transactions: 10
Total Sales: $5,069.90
Average per Transaction: $506.99

2. Single month

Input:
analyze_sales('sales.csv')
Expected Output:
MONTHLY SALES REPORT
==================================================
Month: 2024-05
  Transactions: 3
  Total Sales: $450.00
  Average: $150.00

--------------------------------------------------
OVERALL SUMMARY
Total Transactions: 3
Total Sales: $450.00
Average per Transaction: $150.00

3. Multiple months with varying amounts

Input:
analyze_sales('sales.csv')
Expected Output:
MONTHLY SALES REPORT
==================================================
Month: 2024-06
  Transactions: 2
  Total Sales: $8,500.50
  Average: $4,250.25

Month: 2024-07
  Transactions: 3
  Total Sales: $5,501.00
  Average: $1,833.67

--------------------------------------------------
OVERALL SUMMARY
Total Transactions: 5
Total Sales: $14,001.50
Average per Transaction: $2,800.30
+ 2 hidden test cases