Multifunctional mapper

Report a typo

Wow! This problem is kind of tricky. If you're ready to put your thinking cap on, brace yourself and good luck! Otherwise, you can skip it for now and return any time later.

In this problem, you need to complete the implementation of three functions.

  1. The multifunctionalMapper function that accepts a list of operators (mappers) and returns a new operator. The new operator just accepts a list of integer numbers and sequentially applies each mapper to each number in the list (performs multiple transformations). It produces a list of transformed values.
  2. Based on multifunctionalMapper, implement the multTwoAndThenAddOneTransformation operator that multiplies each integer number by two and then adds one to it. The operator is applied to each number in the input list.
  3. Based on multifunctionalMapper, implement the squareAndThenGetNextEvenNumberTransformation operator that squares each integer number and then calculates the next even number following it. The operator is also applied to each number in the input list.

Here is an example. The identityTransformation is based on the multifunctionalMapper function identity. It doesn't change values in the input list but repeats identity transformation three times, just for the sake of an example.

Let's take a look at two examples of how the operators should work.

Example 1. The input list is [1, 1, 1, 1]

  • identityTransformation returns the list [1, 1, 1, 1]
  • multTwoAndThenAddOneTransformation returns the list [3, 3, 3, 3]
  • squareAndThenGetNextEvenNumberTransformation returns the list [2, 2, 2, 2]

Example 2. The input list is [1, 2, 3].

  • identityTransformation returns the list [1, 2, 3]
  • multTwoAndThenAddOneTransformation returns the list [3, 5, 7]
  • squareAndThenGetNextEvenNumberTransformation returns the result list [2, 6, 10]
Write a program in Java 17
import java.util.Arrays;
import java.util.Scanner;
import java.util.List;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

class MultifunctionalMapper {
private static final String TAB = " ";

/**
* The function accepts a list of mappers and returns an operator that accepts a list of integers
* and sequentially applies each mapper to each value (perform a transformation)
*/
public static final Function<List<IntUnaryOperator>, UnaryOperator<List<Integer>>> MULTIFUNCTIONAL_MAPPER =

/**
* EXAMPLE: the operator transforms each number to the same number (perform the identity transformation)
*
* It returns a list of the same numbers.
*/
public static final UnaryOperator<List<Integer>> IDENTITY_TRANSFORMATION =
MULTIFUNCTIONAL_MAPPER.apply(Arrays.asList(x -> x, x -> x, x -> x));

/**
* The operator accepts an integer list.
* It multiplies by two each integer number and then add one to it.
*
* The operator returns transformed integer list.
*/
public static final UnaryOperator<List<Integer>> MULT_TWO_AND_THEN_ADD_ONE_TRANSFORMATION =

/**
* The operator accepts an integer list.
* It squares each integer number and then get the next even number following it.
*
* The operator returns transformed integer list.
*/
public static final UnaryOperator<List<Integer>> SQUARE_AND_THEN_GET_NEXT_EVEN_NUMBER_TRANSFORMATION =

// Don't change the code below
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
String[] values = scanner.nextLine().split(" ");

List<Integer> numbers = Arrays.stream(values)
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());

List<Integer> idMapper = MULTIFUNCTIONAL_MAPPER
.apply(Arrays.asList(x -> x, x -> x, x -> x))
.apply(numbers);

List<Integer> idTransfomarmation =
IDENTITY_TRANSFORMATION.apply(numbers);

List<Integer> mult2AndAdd1Mapper = MULTIFUNCTIONAL_MAPPER
.apply(Arrays.asList(x -> x * 2, x -> x + 1))
.apply(numbers);

List<Integer> mult2AndAdd1Tranformation =
MULT_TWO_AND_THEN_ADD_ONE_TRANSFORMATION.apply(numbers);

List<Integer> squareAndNextEvenMapper = MULTIFUNCTIONAL_MAPPER
.apply(Arrays.asList(x -> x * x, x -> x % 2 == 0 ? x + 2 : x + 1))
.apply(numbers);

List<Integer> squareAndNextEvenNumberTransformation =
SQUARE_AND_THEN_GET_NEXT_EVEN_NUMBER_TRANSFORMATION.apply(numbers);

StringBuilder result = new StringBuilder("")
.append(getStringFromList(idMapper))
.append(TAB)
.append(getStringFromList(idTransfomarmation))
.append(TAB)
.append(getStringFromList(mult2AndAdd1Mapper))
.append(TAB)
.append(getStringFromList(mult2AndAdd1Tranformation))
.append(TAB)
.append(getStringFromList(squareAndNextEvenMapper))
.append(TAB)
.append(getStringFromList(squareAndNextEvenNumberTransformation))
.append(TAB);

System.out.println(result.toString().trim());
}

private static String getStringFromList(List<Integer> numbers) {
StringBuilder builder = new StringBuilder("");
for (int n : numbers) {
builder.append(n).append(" ");
}
return builder.toString().trim();
}
}
___

Create a free account to access the full topic