Map and Accumulate Learning Queries
Problem statement
For this practice exercise, use the following two operation definitions:
map(items, project)appliesprojectto each item and returns the values in input order.accumulate(items, initial, step)starts withinitialand updates it by applyingstepto each item, from left to right.
You are given car records. Each row of cars contains three strings in this order: [price, color, mileage]. Price and mileage are canonical nonnegative integer decimal strings: only digits, with no leading zeros except for 0. Their units are abstract integer units for this exercise.
Implement runLearningQueries and return exactly three rows:
- Every price string, unchanged and in input order. This is a mapping query.
- One string containing the sum of all mileage values. This is an accumulation query.
- One string
color:countfor each distinct color, sorted by color in ASCII alphabetical order. Count every input record, including duplicates.
Use canonical decimal strings for the sum and counts. For example, zero is 0, not 00. On empty input, return [[],["0"],[]].
The operation definitions describe the required results. You may use ordinary loops or your language's map/fold tools to compute them.
Function
runLearningQueries(cars: String[][]) → String[][]Examples
Example 1
cars = [["100","red","20"],["200","blue","30"],["50","red","5"]]return = [["100","200","50"],["55"],["blue:1","red:2"]]The prices stay in record order. Mileage totals 20 + 30 + 5 = 55. Blue occurs once and red twice, so the grouped row puts blue first.
Example 2
cars = []return = [[],["0"],[]]Mapping no records gives an empty row. Accumulation starts at zero, and there are no colors to count.
Example 3
cars = [["0","a","0"],["1000000","aa","1000000"],["5","a","1"]]return = [["0","1000000","5"],["1000001"],["a:2","aa:1"]]Prices keep their input order, including zero. Mileage totals 1000001. Color a precedes its longer prefix extension aa, and repeated a records both count.
Constraints
0 <= cars.length <= 200.- Every car row contains exactly
3strings. - Every input string contains
1to20characters. - Price and mileage are canonical integer decimal strings with values in
[0, 1000000]. - Each color contains only lowercase ASCII letters
athroughz.