Challenge

Problem

When stakeholders prefer plain text over CSV, proper formatting makes data comprehensible. This drill teaches you to transform data into human-readable text reports with aligned columns, proper spacing, and summary statistics. You'll learn string formatting with printf, dynamic column width calculation, and alignment techniques—essential for command-line tools, email reports, and system administration summaries.

Difficulty: Beginner

Instructions

  1. Read employee data from CSV file (Name, Department, Salary, Years)
  2. Calculate dynamic column widths based on longest values
  3. Format header row with columns aligned
  4. Format data rows with proper alignment:
    • Names: left-aligned
    • Department: left-aligned
    • Salary: right-aligned with $ and commas
    • Years: right-aligned
  5. Add separator line (dashes)
  6. Add summary statistics:
    • Total employees: X
    • Average salary: $XX,XXX
    • Total payroll: $XXX,XXX

Files

Editable
Read-only

Hints

Hint 1

String#ljust(width) left-aligns text with padding

Hint 2

String#rjust(width) right-aligns text with padding

Hint 3

Calculate max width: array.map(&:length).max

Hint 4

Format with commas: number.to_s.reverse.scan(/\d{1,3}/).join(',').reverse

Hint 5

CSV.read(file, headers: true) gives you hash-like access

Hint 6

Use [calculated_width, minimum_width].max for column widths

Provided Files (Read-only)

1. Basic report - 3 employees

Input:
generate_report('employees.csv')
Expected Output:
Name           Department    Salary  Years
---------------------------------------------
John Smith     Engineering  $95,000      5
Alice Johnson  Marketing    $75,000      3
Bob Williams   Sales        $68,000      2
---------------------------------------------
Total employees: 3
Average salary: $79,333
Total payroll: $238,000

2. Single employee

Input:
generate_report('employees.csv')
Expected Output:
Name      Department  Salary  Years
-------------------------------------
Jane Doe  HR         $55,000      1
-------------------------------------
Total employees: 1
Average salary: $55,000
Total payroll: $55,000

3. Large salaries with proper formatting

Input:
generate_report('employees.csv')
Expected Output:
Name       Department   Salary   Years
-----------------------------------------
Sarah Lee  Executive   $250,000     15
Mike Chen  Engineering $125,000      7
-----------------------------------------
Total employees: 2
Average salary: $187,500
Total payroll: $375,000
+ 2 hidden test cases