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, dynamic column width calculation, and alignment techniques—essential for command-line tools, email reports, and system administration summaries. Note: This drill parses CSV manually using string methods rather than Ruby's CSV library.

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 { |r| r[0].length }.max

Hint 4

Format with commas: num.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\1,').reverse

Hint 5

Parse CSV manually: line.split(',')

Hint 6

Use [calculated_width, header_width].max for column widths

Ruby 3.4

Provided Files (Read-only)

1. Correct header alignment

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   Engineering  $105,000     8
Carol Davis    Sales        $68,000      2
-------------------------------------------
Total employees: 4
Average salary: $85,750
Total payroll: $343,000

2. Salaries formatted with commas

Input:
generate_report('employees.csv')
puts 'Formatting verified'
Expected Output:
Name           Department   Salary  Years
-------------------------------------------
John Smith     Engineering  $95,000      5
Alice Johnson  Marketing    $75,000      3
Bob Williams   Engineering  $105,000     8
Carol Davis    Sales        $68,000      2
-------------------------------------------
Total employees: 4
Average salary: $85,750
Total payroll: $343,000
Formatting verified

3. Summary statistics correct

Input:
generate_report('employees.csv')
puts 'Stats verified'
Expected Output:
Name           Department   Salary  Years
-------------------------------------------
John Smith     Engineering  $95,000      5
Alice Johnson  Marketing    $75,000      3
Bob Williams   Engineering  $105,000     8
Carol Davis    Sales        $68,000      2
-------------------------------------------
Total employees: 4
Average salary: $85,750
Total payroll: $343,000
Stats verified
+ 2 hidden test cases