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. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.

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 with formatting
  6. Print overall summary with totals and averages
  7. Format currency with $ sign, commas, and 2 decimal places

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 chronologically

Hint 6

hash.values.flatten combines all arrays into one

Hint 7

Format decimals: '%.2f' % number

Hint 8

Add commas: reverse, gsub, reverse

Ruby 3.4

Provided Files (Read-only)

1. Correct monthly report

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. format_currency works

Input:
puts format_currency(1234.56)
puts format_currency(99.90)
puts format_currency(10000.00)
Expected Output:
$1,234.56
$99.90
$10,000.00

3. Months sorted chronologically

Input:
analyze_sales('sales.csv')
puts 'Sorted OK'
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
Sorted OK
+ 2 hidden test cases