0

how can i change this for loop to recursive in the event that if groupSize=3, n=6 will print 123 124 125 126 134 135 136 145 146 156 234 235 236 245 246 345 346 356 456

public static void printCombinations(int groupSize, int n){

    if (groupSize <=n && groupSize>0 && n>0){

            for (int i=1; i<=n; i++){
                for (int j=1; j<=i-1; j++){
                    for (int k=1; k<=j-1; k++){
                        int first = k;
                        int second = j;
                        int third = i;
                        if (i!=j && i!=k && j!=k){
                            System.out.println(first +" " + second +" "+ third);
                            }
                    }
                }   
            }
        }
    }
user963501
  • 61
  • 2
  • 1. n can't be less than zero of groupSize is greater than 0 and less than n. 2. Your second for loop won't run because the first value of i is 1, so when j is 1, it's going to be greater than 1-1, which is 0. – mowwwalker Oct 01 '11 at 20:49
  • Can you post what you have tried so far? – EkcenierK Oct 01 '11 at 20:49
  • 3
    @Walkerneo: Seems like a reasonable use of recursion to me. – Mark Byers Oct 01 '11 at 20:50
  • @MarkByers, yeah, I thought he was doing something else initially – mowwwalker Oct 01 '11 at 20:51
  • OK, so you're trying to print all numbers with the amount of digits in groupSize using the numbers 1 through n such that the the digit in the hundred's place is less than the digit in the tens is less than the digit in the ones? – mowwwalker Oct 01 '11 at 20:56
  • @Joh: Of course it's homework, why even ask? – Hovercraft Full Of Eels Oct 01 '11 at 21:01
  • @HovercraftFullOfEels Don't like to retag to homework without confirmation since that can affect how answers are given – John B Oct 01 '11 at 21:04
  • @Joh: Don't retag it. Please read the meta discussions on this as this has been discussed over and over and the large majority strongly advise against re-tagging with meta tags like homework. For instance, check this link: [definitive-answer-regarding-retagging-homework-questions](http://meta.stackexchange.com/questions/79475/definitive-answer-regarding-retagging-homework-questions) – Hovercraft Full Of Eels Oct 01 '11 at 21:06

2 Answers2

1

Probably going to be rough around the edges; I'm just learning Java.

class comboMaker {
    public static void main(String args[]){
        String[] test1 = startCombo(3,6);
        prnt(test1);
    }
    private static String[] startCombo(int len,int digits){
        return combos(len,digits,"",0);
    }
    private static String[] combos(int len,int digits,String cur,int lastdig){
        if (len>digits){
            return null;
        }
        if (cur.length()==len){
            return new String[]{cur};           
        }
        String tmp = cur;
        String[] rtn = {};
        for(int i = lastdig+1;i<=digits;i++){
            tmp=cur+Integer.toString(i);
            rtn=concat(rtn,combos(len,digits,tmp,i));   
        }
        return rtn;

    }
    private static String[] concat(String[] A, String[] B) {
           String[] C= new String[A.length+B.length];
           System.arraycopy(A, 0, C, 0, A.length);
           System.arraycopy(B, 0, C, A.length, B.length);
           return C;
    }

    private static void prnt(String[] str){
        if(str==null){
            System.out.println("Invalid string array");
            return;
        }
        for(int i =0;i<str.length;i++ ){
            System.out.println(str[i]);
        }   
    }   
}

I included a method to concatenate arrays that I found here: How can I concatenate two arrays in Java?

Also a method to print your string array generated with the combo maker.

Community
  • 1
  • 1
mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • considering this was probably homework, it would have probably been better not to have given such a complete / explicit answer. – John B Oct 02 '11 at 03:21
  • Maybe it's not homework. Maybe this will help him when he otherwise would have failed the class. I honestly have no clue, but I'm trying to learn Python and Java, so I just wanted the experience. – mowwwalker Oct 02 '11 at 03:33
  • Thank everyone for the example given, I am in the process of learning java and this is about to learn recursion, the code above is written in not a good way though if I change the groupSize to 4, it will not work. So, I plant to write it in recursion way. =) – user963501 Oct 02 '11 at 04:27
  • Output if using comboStart(4,6): `1234 1235 1236 1245 1246 1256 1345 1346 1356 1456 2345 2346 2356 2456 3456 ` The length of the numbers generated can't be more than the number of digits given, or else there aren't enough digits for each value to be greater than the last. – mowwwalker Oct 02 '11 at 04:32
  • sorry Walkerneo, the code i mentioned is the code i wrote, your code is working fine. – user963501 Oct 02 '11 at 05:12
  • 1
    user963501: If you found this answer to be helpful, you should give @Walkerneo credit – John B Oct 02 '11 at 13:42
0

Create a method that will produce the suffix to a particular value with x digits and return a list of all possible suffixes. Might be something like:

List<String> generateSuffixes(int prefix, int suffixSize, int maxDigitValue);

The method would iterate from prefix + 1 to maxDigitValue. It would then call itself with the iterate value and suffixSize - 1. It would append the iterate to each suffix generated to create the list to return.

John B
  • 32,493
  • 6
  • 77
  • 98