Friday, February 24, 2023
HomeSoftware EngineeringEasy methods to Fold an Array in Java

# Easy methods to Fold an Array in Java

## The problem#

On this problem, you must write a way that folds a given array of integers by the center x-times.

An instance says greater than a thousand phrases:

``````Fold 1-times:
[1,2,3,4,5] -> [6,6,3]

A little bit visualization (NOT for the algorithm however for the concept of folding):

Step 1         Step 2        Step 3       Step 4       Step5
5/           5|         5
4/            4|          4
1 2 3 4 5      1 2 3/         1 2 3|       1 2 3       6 6 3
----*----      ----*          ----*        ----*        ----*

Fold 2-times:
[1,2,3,4,5] -> [9,6]
``````

As you see, if the depend of numbers is odd, the center quantity will keep. In any other case, the fold-point is between the middle-numbers, so all numbers could be added in a method.

The array will all the time comprise numbers and can by no means be null. The parameter runs will all the time be a constructive integer better than 0 and say what number of runs of folding your methodology has to do.

If an array with one component is folded, it stays as the identical array.

The enter array shouldn’t be modified!

## The answer in Java code#

Choice 1:

``````public class Answer {
public static int[] foldArray(int[] a, int r) r==1 ? f : foldArray(f,--r);

}
``````

Choice 2:

``````import java.util.Arrays;
import java.util.stream.IntStream;

public static int[] foldArray(int[] array, int runs) {
closing int[] consequence = Arrays.copyOfRange(array, 0, Math.spherical(array.size / 2.0f));
IntStream.vary(0, array.size / 2)
.forEach(i -> consequence[i] = array[i] + array[array.length - 1 - i]);
return runs > 1 ? foldArray(consequence, --runs) : consequence;
}
}
``````

Choice 3:

``````import java.util.*;

public static int[] foldArray(int[] array, int runs) {
int[] arr = Arrays.copyOf(array, array.size);
int sure = arr.size;
for (int ok = 0; ok < runs; ok++) {
for (int i = 0; i < sure / 2; i++) {
arr[i] += arr[bound - 1 - i];
}
sure = (sure + 1) / 2;
}
return Arrays.copyOf(arr, sure);
}
}
``````

## Take a look at circumstances to validate our resolution#

``````import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.Arrays;

public class SolutionTests {
@Take a look at
public void basicTests() {
int[] enter = new int[] { 1, 2, 3, 4, 5 };
int[] anticipated = new int[] { 6, 6, 3 };

anticipated = new int[] { 9, 6 };

anticipated = new int[] { 15 };

enter = new int[] { -9, 9, -8, 8, 66, 23 };
anticipated = new int[] { 14, 75, 0 };
}

@Take a look at
public void extendedTests() {
int[] enter = new int[] { 1, 2, 3, 4, 5, 99, 88, 78, 74, 73 };
assertEquals(Arrays.toString(new int[] { 427 }), Arrays.toString(Answer.foldArray(enter, 5)));

enter = new int[] { -1, -2, -3, -4, -5, -99, -88, -78, -74, -73 };
assertEquals(Arrays.toString(new int[] { -427 }), Arrays.toString(Answer.foldArray(enter, 5)));

enter = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
assertEquals(Arrays.toString( new int[] { 0 }), Arrays.toString(Answer.foldArray(enter, 10)));

enter = new int[] { 2 };

enter = new int[] { 2 };
}

interface FoldArrayInterface {
int[] foldArray(int[] array, int runs);
}

@Take a look at
public void randomTests() {

FoldArrayInterface myMethod = new FoldArrayInterface() {
public int[] foldArray(int[] array, int runs) {
int size = (int)Math.ceil(array.size/2.0);
int[] foldedArray = new int[length];

for(int i=0;i<size;i++) {
if(i != array.size - 1 - i) {
foldedArray[i] = array[i] + array[array.length - 1 - i];
} else {
foldedArray[i] = array[i];
}
}

if(runs == 1) {
return foldedArray;
}
return foldArray(foldedArray, runs - 1);
}
};

for(int r=0;r<20;r++) {
int size = (int)(Math.random() * 199 + 1);
int runs = (int)(Math.random() * 19 + 1);
int[] enter = new int[length];
for(int i=0;i<size;i++) {
enter[i] = (int)(Math.random() * 401 - 200);
}

int[] anticipated = myMethod.foldArray(enter, runs);
}
}
}
``````

RELATED ARTICLES