Skip to content

Commit a89d989

Browse files
authored
Merge pull request #103 from Umutayb/list-partition-utility
Update ArrayUtilities.java
2 parents 45eef81 + 4974a7e commit a89d989

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/main/java/utils/arrays/ArrayUtilities.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package utils.arrays;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.Random;
56

@@ -40,5 +41,41 @@ public static <ItemType> ItemType getRandomItemFrom(List<ItemType> items) {
4041
public static <ItemType> boolean isLastMemberOf(List<ItemType> items, ItemType member){
4142
return items.indexOf(member) == (items.size() - 1);
4243
}
44+
45+
/**
46+
* Splits a given list into partitions and returns a specific partition based on the partition index.
47+
*
48+
* This method uses stream filtering to find and return the elements that belong to the requested partition.
49+
* The partition index is 0-based.
50+
*
51+
* @param <T> The type of elements in the list.
52+
* @param originalList The original list to be partitioned.
53+
* @param partitionSize The size of each partition.
54+
* @param partitionIndex The index of the partition to retrieve (0-based index).
55+
* @return A list containing the elements from the specified partition.
56+
* @throws IndexOutOfBoundsException If the partitionIndex is out of bounds (negative or too large).
57+
*/
58+
public static <T> List<T> getListPartition(List<T> originalList, int partitionSize, int partitionIndex){
59+
int partitionBottomLimit = partitionSize * partitionIndex;
60+
return originalList.stream().filter(item -> {
61+
int itemIndex = originalList.indexOf(item);
62+
return partitionBottomLimit <= itemIndex && itemIndex < partitionSize + partitionBottomLimit;
63+
}).toList();
64+
}
65+
66+
/**
67+
* Calculates the total number of partitions needed to divide a list of a given size into partitions
68+
* of the specified size.
69+
*
70+
* If there are leftover elements after dividing the list, an extra partition is counted.
71+
*
72+
* @param listSize The total number of elements in the list.
73+
* @param partitionSize The size of each partition.
74+
* @return The number of partitions required to divide the list.
75+
*/
76+
public static int getPartitionCount(int listSize, int partitionSize){
77+
int partitionCount = listSize / partitionSize;
78+
return listSize % partitionCount == 0 ? partitionCount : partitionCount + 1;
79+
}
4380
}
4481

src/test/java/AppTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.List;
2020
import java.util.regex.Pattern;
2121

22-
import static utils.arrays.ArrayUtilities.isLastMemberOf;
22+
import static utils.arrays.ArrayUtilities.*;
2323
import static utils.email.EmailUtilities.Inbox.EmailField.CONTENT;
2424
import static utils.email.EmailUtilities.Inbox.EmailField.SUBJECT;
2525
import static utils.MappingUtilities.Json.*;
@@ -241,12 +241,39 @@ public void setFieldTest() {
241241
expectedFieldValue,
242242
pet.getName()
243243
);
244-
printer.success("The setFieldTest() test passed!");
244+
printer.success("The setFieldTest() test pass!");
245245
}
246246

247247
@Test
248248
public void lastItemOfTest() {
249249
List<Integer> integers = List.of(1, 2, 3, 4, 5);
250250
Assert.assertTrue("Integer was not the last member!", isLastMemberOf(integers, 5));
251+
printer.success("The lastItemOfTest() test pass!");
252+
}
253+
254+
@Test
255+
public void partitionTest() {
256+
List<Integer> integers = List.of(1, 2, 3, 4, 5);
257+
List<List<Integer>> partitionLists = List.of(
258+
List.of(1,2),
259+
List.of(3,4),
260+
List.of(5)
261+
);
262+
for (int i = 0; i < getPartitionCount(integers.size(), 2); i++) {
263+
List<Integer> partition = getListPartition(integers, 2, i);
264+
Assert.assertEquals(partition, partitionLists.get(i));
265+
}
266+
printer.success("The partitionTest() test pass!");
267+
}
268+
269+
@Test
270+
public void partitionCountTest() {
271+
List<Integer> integers = List.of(1, 2, 3, 4, 5);
272+
Assert.assertEquals(
273+
"The getPartitionCount() method returns an incorrect value!",
274+
3,
275+
getPartitionCount(integers.size(), 2)
276+
);
277+
printer.success("The partitionCountTest() test pass!");
251278
}
252279
}

0 commit comments

Comments
 (0)