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
MediumThe 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 above70(useall?).: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:
- The qualified IDs joined with
,, prefixed withQualified:. If none, printQualified: (none). - 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: 1Try 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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String Methods OverviewString InterpolationIterating Over StringsSplit and JoinRecap - String Weaver4Blocks, Procs & Lambdas
What is a Block?do..end vs BracesThe yield KeywordBlock ParametersProcs and LambdasRecap - Custom Iterator7Hashes Part 2
Hash.new with DefaultsIterating HashesNested HashesMerging and TransformingRecap - Frequency Counter10Project - Student Records
Project OverviewAdd Student5Enumerable Powerhouse
Select and RejectChaining MapReduce / Injectcount, all?, any?, none?group_by and partitionsort_by, min_by, max_byRecap - Data Pipeline8Advanced Decision Making
Case with Classes & RegexMulti-value whenTernary OperatorInline if / unlessRecap - Grade Classifier