Recap - List Processor
Part of the Logic & Flow section of Coddy's Dart journey — lesson 50 of 65.
Challenge
EasyCreate 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.
- Read a string input containing numbers separated by commas (e.g.,
"10,20,30,40") - Read a string input representing the first operation type (
"multiply","add", or"subtract") - Read a string input representing the value to use with the first operation (e.g.,
"3") - Read a string input representing the second operation type (
"multiply","add", or"subtract") - Read a string input representing the value to use with the second operation (e.g.,
"5") - Split the first input into individual numbers and convert each to an integer
- Create three operation functions:
multiplyBy- takes two integers and returns the first multiplied by the secondaddTo- takes two integers and returns the first plus the secondsubtractFrom- takes two integers and returns the first minus the second- Create a main function called
processListWithOperationthat 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
- The
processListWithOperationfunction should apply the given function to each number in the list using the provided value and return a new list with the results - Apply the first operation to the original list, then apply the second operation to the result of the first operation
- 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 successfullyIf 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 successfullyYour 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
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update7Advanced Functions
Anonymous FunctionsPassing Functions as ArgumentsUnderstanding ClosuresIntroduction to RecursionRecursive Function: CountdownRecursive Function: FactorialRecap - List Processor2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List