Question

I implemented a recursive algorithm in C # that lists all possible combinations of N from K , it wroked very well, very reliable results, now I needed to implement it in java, I did , but the results are missing? many cases are not listed, after trying to see more than once where is the problem , I want you to try to see the problem with me. Thank you

C# Code :

static List<string> Combinations(List<string> motList, int Longeur)
{
    List<String> Resultat = new List<string>();
    for (int i = 0; i < motList.Count; i++)
    {
        if (Longeur == 1)
            Resultat.Add(motList[i]);
        else
        {
            List<string> ListIntermediaire = motList.GetRange(i + 1, motList.Count - (i + 1));
            List<string> CombiList = Combinations(ListIntermediaire, Longeur - 1);
            foreach (string s in CombiList)
                Resultat.Add(motList[i] + s);
        }
    }
    return Resultat;
}

Java code :

 //function to get sub arrayList (getRange in c#)
    public static ArrayList subArrayList (ArrayList ls , int i , int j)
    {
            ArrayList res = new ArrayList ();
            for (int k = i; k <= j ; k++) {
                res.add(ls.get(k));
            }
            return  res;
     }

     //Java version of combination function 
     public static ArrayList Combinaison (ArrayList motList, int Longeur)
     {
            ArrayList Resultat = new ArrayList();
            for (int i = 0; i <motList.size() ; i++) {      
                if (1 == Longeur )
                    Resultat.add(motList.get(i));
                else
                {  
                    ArrayList ListIntermediaire  = subArrayList(motList,i+1 , motList.size()-(i+1));  
                    ArrayList CombiList = Combinaison(ListIntermediaire, Longeur-1);
                    for (int j = 0; j < CombiList.size(); j++)
                          Resultat.add( motList.get(i) +""+ CombiList.get(j) );                    
                }   
            }  
            return  Resultat;
       }
Was it helpful?

Solution

Okay yes I see your point. Solution for combinations (and not permutations):

public static void main(final String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");
    List<String> result = getCombinations(list, 2);
    System.out.println(result.size());
    System.out.println(result);
}

private static List<String> getCombinations(final List<String> list, final int length) {
    if (length >= 1) {
        return removeUntilLength(list, length, 0);
    }
    return new ArrayList<>();
}

private static List<String> removeUntilLength(final List<String> list, final int length, final int lastIdx) {
    List<String> ret = new ArrayList<>();
    if (list.size() == length) {
        ret.add(list.toString());
    } else {
        for (int i = lastIdx; i < list.size(); i++) {
            List<String> tmp = new ArrayList<>(list);
            tmp.remove(i);
            ret.addAll(removeUntilLength(tmp, length, Math.max(i, 0)));
        }
    }
    return ret;
}

OTHER TIPS

Your code is not very much adhering to Java coding standards, I mashed up another solution if this was any help:

public static void main(final String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");
    List<String> result = getCombinations(list, 3);
    System.out.println(result);
}

private static List<String> getCombinations(final List<String> list, final int length) {
    List<String> ret = new ArrayList<>();
    if (length >= 1) {
        for (int j = 0; j < list.size(); j++) {
            List<String> tmp = new ArrayList<>(list);
            String excluded = tmp.remove(j);
            List<String> remainingCombinations = getCombinations(tmp, length - 1);
            if (remainingCombinations.size() > 0) {
                for (String s : remainingCombinations) {
                    String combination = excluded + s;
                    ret.add(combination);
                }
            } else {
                ret.add(excluded);
            }
        }
    }
    return ret;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top