Menu
Coddy logo textTech

Filtering with 'where'

Part of the Logic & Flow section of Coddy's Dart journey — lesson 11 of 65.

While map() transforms every element in a list, sometimes you only want to keep elements that meet specific criteria. The where() method allows you to filter a list by testing each element against a condition and keeping only those that pass the test.

The where() method takes a test function that returns true or false for each element. Elements that return true are included in the result, while those that return false are filtered out.

List<int> scores = [45, 78, 92, 33, 67, 88];

// Filter to keep only scores above 50
var passingScores = scores.where((score) => score > 50);
print(passingScores);  // (78, 92, 67, 88)

This filtering approach is much cleaner than manually looping through a list and building a new collection. The where() method represents a fundamental pattern in data processing, allowing you to declaratively specify what you want to keep rather than how to extract it.

challenge icon

Challenge

Easy

Create a program that manages a student performance tracking system by filtering students based on their test scores. Your program should:

  1. Read a string input representing the class name
  2. Read multiple integer inputs representing student test scores (the input will end when you receive -1)
  3. Read an integer input representing the minimum passing score threshold
  4. Use the where() method to filter and keep only the scores that are greater than or equal to the threshold
  5. Print the filtering results in the exact format shown below

For example, if the class name is "Mathematics 101", the scores are 85, 42, 78, 91, 55, 88, and the threshold is 70, your program should output:

Class: Mathematics 101
Original scores: [85, 42, 78, 91, 55, 88]
Minimum passing score: 70
Filtering scores >= 70
Passing scores: (85, 78, 91, 88)
Students passed: 4 out of 6
Pass rate: 66.67%

If the class name is "Science Lab", the scores are 45, 38, 52, and the threshold is 60, your program should output:

Class: Science Lab
Original scores: [45, 38, 52]
Minimum passing score: 60
Filtering scores >= 60
Passing scores: ()
Students passed: 0 out of 3
Pass rate: 0.00%

If the class name is "History", the scores are 95, 88, 92, and the threshold is 80, your program should output:

Class: History
Original scores: [95, 88, 92]
Minimum passing score: 80
Filtering scores >= 80
Passing scores: (95, 88, 92)
Students passed: 3 out of 3
Pass rate: 100.00%

Your program must use the where() method to filter scores that are greater than or equal to the threshold. The filtered result will be displayed in parentheses format as an iterable. Calculate the pass rate as a percentage with exactly 2 decimal places using the formula: (passing students / total students) * 100.

Cheat sheet

The where() method filters a list by keeping only elements that meet specific criteria. It takes a test function that returns true or false for each element.

List<int> scores = [45, 78, 92, 33, 67, 88];

// Filter to keep only scores above 50
var passingScores = scores.where((score) => score > 50);
print(passingScores);  // (78, 92, 67, 88)

Elements that return true are included in the result, while those that return false are filtered out. This provides a cleaner alternative to manually looping through lists.

To format a decimal number to a fixed number of decimal places, use toStringAsFixed(n), where n is the number of decimal places you want:

double value = 3.14159;
print(value.toStringAsFixed(2));  // 3.14

Try it yourself

import 'dart:io';

void main() {
  // Read class name
  String? className = stdin.readLineSync();
  
  // Read scores until -1 is encountered
  List<int> scores = [];
  while (true) {
    int score = int.parse(stdin.readLineSync()!);
    if (score == -1) break;
    scores.add(score);
  }
  
  // Read minimum passing score threshold
  int threshold = int.parse(stdin.readLineSync()!);
  
  // TODO: Write your code below
  // Use the where() method to filter scores >= threshold
  
  // Print the results in the required format
  print('Class: $className');
  print('Original scores: $scores');
  print('Minimum passing score: $threshold');
  print('Filtering scores >= $threshold');
  // Print passing scores, students passed count, and pass rate
}
quiz iconTest yourself

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

All lessons in Logic & Flow