Menu
Coddy logo textTech

Recap - List Processor

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

challenge icon

Challenge

Easy

Create a flexible data processing system that demonstrates higher-order functions by building a customizable list processor. Your program will create different processing functions and use a main processor function that can apply any operation to a list of data.

  1. Read a string input containing numbers separated by commas (e.g., "10,20,30,40")
  2. Read a string input representing the first operation type ("multiply", "add", or "subtract")
  3. Read a string input representing the value to use with the first operation (e.g., "3")
  4. Read a string input representing the second operation type ("multiply", "add", or "subtract")
  5. Read a string input representing the value to use with the second operation (e.g., "5")
  6. Split the first input into individual numbers and convert each to an integer
  7. Create three operation functions:
    • multiplyBy - takes two integers and returns the first multiplied by the second
    • addTo - takes two integers and returns the first plus the second
    • subtractFrom - takes two integers and returns the first minus the second
  8. Create a main function called processListWithOperation that takes three parameters:
    • A list of integers
    • A function that takes two integers and returns an integer
    • An integer value to use with the operation
  9. The processListWithOperation function should apply the given function to each number in the list using the provided value and return a new list with the results
  10. Apply the first operation to the original list, then apply the second operation to the result of the first operation
  11. Display the results in the exact format shown below

For example, if the inputs are "2,4,6", "multiply", "3", "add", and "10", your program should output:

Advanced List Processor
=======================
Original list: [2, 4, 6]
First operation: multiply by 3
After first operation: [6, 12, 18]
Second operation: add 10
Final result: [16, 22, 28]
Processing completed successfully

If the inputs are "15,25,35", "subtract", "5", "multiply", and "2", your program should output:

Advanced List Processor
=======================
Original list: [15, 25, 35]
First operation: subtract 5
After first operation: [10, 20, 30]
Second operation: multiply by 2
Final result: [20, 40, 60]
Processing completed successfully

Your program must demonstrate higher-order functions by passing the appropriate operation function to processListWithOperation based on the operation type inputs. Use conditional statements to determine which function to pass for each operation. The processListWithOperation function should iterate through the input list, apply the given function with the provided value to each element, and collect the results in a new list. Apply the operations sequentially, using the result of the first operation as input for the second operation.

Try it yourself

import 'dart:io';

void main() {
  // Read inputs
  String? numbersInput = stdin.readLineSync();
  String? firstOperation = stdin.readLineSync();
  String? firstValue = stdin.readLineSync();
  String? secondOperation = stdin.readLineSync();
  String? secondValue = stdin.readLineSync();
  
  // Convert string input to list of integers
  List<int> originalList = numbersInput!.split(',').map((e) => int.parse(e.trim())).toList();
  int firstOpValue = int.parse(firstValue!);
  int secondOpValue = int.parse(secondValue!);
  
  // TODO: Write your code below
  // Create the three operation functions: multiplyBy, addTo, subtractFrom
  // Create the processListWithOperation function
  // Apply the operations sequentially and store results
  
  // Output the results in the required format
  print('Advanced List Processor');
  print('=======================');
  print('Original list: $originalList');
  // Print first operation and result
  // Print second operation and final result
  print('Processing completed successfully');
}

All lessons in Logic & Flow