-1

I have been having some issues getting the logic down for the following:

Simplifying, I have an N-element list of Skids defined as:

public class Skid {
    public Integer length { get; set; }
    public Integer width { get; set; }
    public Boolean turnable { get; set; }
}

I want to generate a List of lists of Skids, where each combination of a skid that is turnable (length / width get switched) is accounted for.

Ex.

Input List<Skid>:

[Skid(20,30,true), Skid(30,40,true), Skid(25,25, false)]

This input should be checked for skids that can be turned, and each combination of a turned skid creates a new list. I am looking for all possible ways to organize the list of skids.

Output List<List<Skid>>:

[[Skid(20,30,true), Skid(30,40,true), Skid(25,25, false)],
    [Skid(30,20,true), Skid(30,40,true), Skid(25,25, false)],
    [Skid(30,20,true), Skid(40,30,true), Skid(25,25, false)],
    [Skid(20,30,true), Skid(40,30,true), Skid(25,25, false)]
    ]

I have been trying to get this to come together using recursion, but am having a hard time wrapping my head around it. How do I generate this output?

Thanks in advance!

  • You want something like a Map<Boolean(turnable), List<Skid>>? am I understanding that correctly? – EricSSH May 23 at 21:09
  • Looking for List<List<Skid>> as the result, where each inner list is a variant of the original. – shawn May 23 at 21:12
  • 1
    What is the variant of the original? – EricSSH May 23 at 21:15
  • 2
    Please edit your post to be specific about expected input/output and what it means to make that transformation. Also, please include an actual question. Thanks! – Adrian Larson May 23 at 21:16
  • I have edited the original question, hopefully it is more clear now. Sorry! – shawn May 23 at 21:31
0

Just wanted to post an update with the answer to my question. Sorry if my explanation in the question was confusing, I will try to be more clear in future questions.

private static void generateCombinations_recurse(List<CommunitySkid[]> all_lists, List<CommunitySkid> accum, List<CommunitySkid> rest) {

     if(rest.isEmpty())
    {
        if(accum.size() > 0)
            all_lists.add(CommunitySkid.deepClone(accum));    

        return;
    }

    rest = CommunitySkid.deepClone(rest);

    CommunitySkid next = rest.remove(0);        
    //Now clone to get new Ids
    List<CommunitySkid> accum_1 = CommunitySkid.deepClone(accum);
    accum_1.add(next);
    generateCombinations_recurse(all_lists, accum_1, rest);        

    if(next.turnable && next.length != next.width) {
        CommunitySkid n2 = next.clone2();
        n2.turn();
        accum = CommunitySkid.deepClone(accum);
        accum.add(n2);
        generateCombinations_recurse(all_lists, accum, rest);
    }        
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.