mvg
mvg

Reputation: 1584

Unable to break from a recursive code correctly

I wrote a code that splits a list of integers to a list of list based upon a fixed sum. For example if the input is [1,2,3,5] and the fixed size is 5. Then the output must be [[1,2,2],[1,4],[1]].

To achieve this I wrote a code as below

public class SplitListtoLists {

 public static void main(String[] args) {
    int[] intArr = {1,2,3,5};//[[1,2,2],[1,4],[1]]
    List<Integer> aList = new ArrayList<Integer>();
    aList.add(1);
    aList.add(2);
    aList.add(3);
    aList.add(5);

    System.out.println(aList);

    List<List<Integer>> aListOfList = splitList(aList,0,new ArrayList<>());
    System.out.println(aListOfList);
}

public static List<List<Integer>> splitList(List<Integer> aList, int accCount, List<List<Integer>> listOfList) {
    List<Integer> targetList = new ArrayList<>();
    for (int i = 0; i <aList.size() ; i++) {
        int number = aList.get(i);
        System.out.format("\t i=%d aList=%s targetList=%s number=%d accCount=%d \n",i,aList,targetList,number,accCount);
        System.out.format("\t list of list %s\n",listOfList);
        accCount = accCount + number;
        int batchSize = 5;
        if(accCount < batchSize) {
            targetList.add(number);
        } else if(accCount > batchSize){
            int limitedCount = accCount - batchSize;
            int currentArrNumber = number - limitedCount;
            targetList.add(currentArrNumber);
            listOfList.add(targetList);
            List<Integer> subList = new ArrayList<>();
            subList.add(limitedCount);
            subList.addAll(aList.subList(i+1,aList.size()));
            splitList(subList, 0, listOfList);
            System.out.println("\t\tAfter recursion "+listOfList);
            System.out.println("\t\ti "+i);
            aList = new ArrayList<>();
        } else {
            targetList.add(number);
            listOfList.add(targetList);
            targetList = new ArrayList<>();
            accCount = 0;
        }
    }
    listOfList.add(targetList);

    return listOfList;
}


}

But the recursion loop runs even after I got the expected output.
This is the output I am getting now

[[1, 2, 2], [1, 4], [1], [1, 4], [1, 2, 2]]

How to stop it correctly after [1]?

Thanks in advance

Upvotes: 0

Views: 58

Answers (1)

ecerer
ecerer

Reputation: 143

Crazy Code ;) I hope it's correct now!

To add a list, is the problem, u need a copy of it...

public static List<List<Integer>> splitList(List<Integer> aList, int accCount, List<List<Integer>> listOfList){
    List<Integer> targetList = new ArrayList<>();
    for(int i = 0; i < aList.size(); i++){
        int number = aList.get(i);
        System.out.format("\t i=%d aList=%s targetList=%s number=%d accCount=%d \n", i, aList, targetList, number, accCount);
        System.out.format("\t list of list %s\n", listOfList);
        accCount = accCount + number;
        int batchSize = 5;
        if(accCount < batchSize){
            targetList.add(number);
        }else if(accCount > batchSize){
            int limitedCount = accCount - batchSize;
            int currentArrNumber = number - limitedCount;
            targetList.add(currentArrNumber);
            listOfList.add(List.copyOf(targetList));
            List<Integer> subList = new ArrayList<>();
            subList.add(limitedCount);
            subList.addAll(aList.subList(i + 1, aList.size()));
            splitList(subList, 0, listOfList);
            System.out.println("\t\tAfter recursion " + listOfList);
            System.out.println("\t\ti " + i);
            aList = new ArrayList<>();
            targetList.clear();
        }else{
            targetList.add(number);
            listOfList.add(targetList);
            targetList = new ArrayList<>();
            accCount = 0;
        }
    }
    if(!targetList.isEmpty())listOfList.add(targetList);
    
    return listOfList;
}

edit: even better is List.copyOf(targetList) ( i changed the code too)

Upvotes: 1

Related Questions