Menu
Coddy logo textTech

Data Transformer

Part of the Logic & Flow section of Coddy's Ruby journey — lesson 56 of 56.

The capstone challenge, reshape a dataset into a summary report. Combine filtering, mapping, grouping, error handling, and method definition into one program.

challenge icon

Challenge

Medium

The array students is given. Each entry is a hash with :id, :grades (an array of integers, possibly empty), and :subjects (an array of strings).

Define a method transform_dataset(students) that returns a hash with two keys:

  • :qualified_ids: sorted array of :ids of students who have at least one grade and all grades strictly above 70 (use all?).
  • :subject_counts: a hash mapping each subject taken by the qualified students to the number of qualified students taking it.

If a student's :grades array is empty, they are not qualified, silently skip them.

Then call the method and print:

  1. The qualified IDs joined with , , prefixed with Qualified: . If none, print Qualified: (none).
  2. One line per subject (sorted alphabetically) in the format <subject>: <count>.

For the default students, the output is:

Qualified: S1, S4
biology: 1
history: 1
math: 2
physics: 1

Cheat sheet

Combine filtering, mapping, grouping, and output formatting in Ruby:

def transform_dataset(students)
  qualified = students.select do |s|
    s[:grades].any? && s[:grades].all? { |g| g > 70 }
  end

  qualified_ids = qualified.map { |s| s[:id] }.sort

  subject_counts = Hash.new(0)
  qualified.each do |s|
    s[:subjects].each { |subj| subject_counts[subj] += 1 }
  end

  { qualified_ids: qualified_ids, subject_counts: subject_counts }
end

Print results with fallback and sorted keys:

result = transform_dataset(students)

ids = result[:qualified_ids]
puts "Qualified: #{ids.any? ? ids.join(', ') : '(none)'}"

result[:subject_counts].sort.each do |subject, count|
  puts "#{subject}: #{count}"
end
  • all? returns false on empty arrays — use any? first to guard against empty grades.
  • Hash.new(0) initializes missing keys to 0, useful for counting.
  • .sort on a hash returns sorted [key, value] pairs alphabetically.

Try it yourself

students = [
  { id: "S1", grades: [85, 92, 78], subjects: ["math", "physics"] },
  { id: "S2", grades: [65, 80, 90], subjects: ["math", "biology"] },
  { id: "S3", grades: [],          subjects: ["art"] },
  { id: "S4", grades: [88, 95],    subjects: ["math", "biology", "history"] },
  { id: "S5", grades: [70, 75],    subjects: ["chemistry"] }
]

# TODO: def transform_dataset(students), return { qualified_ids:, subject_counts: }
# TODO: print qualified IDs and subject counts
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow