-4

I have been trying to generate a Java code for this, but I need help. I have a survey with 10 questions and each question can be answered with a number between 1 and 5. I need to create a list of all possible combinations. All questions are mandatory.

With this, I have the following:

Questions: 10

Answers: 1,2,3,4,5

The desired output is like this:

1111111111
1111111112
1111111113
1111111114
1111111115
1111111121
1111111122
1111111123
1111111124
1111111125
1111111131
1111111132
1111111133
1111111134
1111111135
1111111141
1111111142
. .
5555555555

Optionally the code can have the option to change number of questions: instead of fixed to 10, change it to 6 or 12, and also the number of answers: instead of only 1 to 5, change it to 1 to 4.

This is the code I wrote:

try {
    PrintWriter pw = new PrintWriter(new FileWriter(
            "C:\Users\User\Desktop\Test\prueba.txt"));

    int numResultados = 20;
    int numPreguntas = 10;
    int respuestaMax = 5;
    int respuestaMin = 1;

    int[] input = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
    int posicion = 0;

    for (int k = 0; k <= numResultados; k++) {
        for (int j = 0; j < respuestaMax; j++) {
            for (int i = 0; i < input.length; i++) {
                System.out.print(input[i]);
            }
            System.out.println();
            input[posicion]++;
        }
        input[posicion] = 1;
        posicion++;
    }
} catch (Exception e) {
    e.printStackTrace();
}

but the output I receive is not the one I'm expecting and I'm stucked, extract:

1111111111
2111111111
3111111111
4111111111
5111111111
1111111111
1211111111
1311111111
1411111111
1511111111
1111111111
1121111111
1131111111
1141111111
1151111111
1111111111
1112111111
1113111111
1114111111
1115111111
Community
  • 1
  • 1

2 Answers2

0

It should be implemented using recursion, by creating a prefix and appending it until all places are filled:

static void printNum(int[] digits, int len, String prefix) {
    if (len == 0) { // all places are filled, print the result
        System.out.println(prefix); 
        return;
    }
    for (int i = 0; i < digits.length; i++) {
        printNum(digits, len - 1, prefix + digits[i]);
    }
}

// test method: 4 questions with 3 possible answers
public static void main(String args[]) {
    int questionNum = 4;
    int[] answers = {1, 2, 3};
    
    printNum(answers, questionNum, "");
}

Output:

1111
1112
1113
1121
1122
1123
1131
1132
1133
1211
...
3321
3322
3323
3331
3332
3333

Update
There are the following issues with existing code:

  1. input array should be printed from the last index to 0 (in reverse order of index)
  2. "carry" into more-significant place is not implemented, only reset in current posicion to 1 which causes duplicate 1111..11 and posicion needs to be reset.
  3. k should be incremented as soon as the set of answers is printed.

These issues may be resolved as follows:

int numPreguntas = 4;
int respuestaMax = 5;
int respuestaMin = 1;
int numResultados = 20;

int[] input = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int posicion = 0;

for (int k = 0; k < numResultados; ) {
    for (int j = 1; j <= respuestaMax; j++) {
        for (int i = input.length - 1; i >= 0; i--) {
            System.out.print(input[i]);
        }
        System.out.println();
        input[posicion]++;
        k++;
    }
    do { // fix carry
        input[posicion] = 1;
        if (++posicion < input.length) // prevent ArrayOutOfBounds
            input[posicion] += 1;
        else break;
    }
    while (input[posicion] > respuestaMax);
    posicion = 0; // reset position
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
0

You can generate a 2d array of possible combinations using map and reduce methods. In this case, the number of possible combinations is 510 = 9765625.

int questions = 10;
int[] answers = {1, 2, 3, 4, 5};
int[][] combinations = IntStream.range(0, questions)
        // prepare 2d arrays of questions and answers
        .mapToObj(i -> Arrays.stream(answers)
                .mapToObj(j -> new int[]{j})
                // stream of 2d arrays
                .toArray(int[][]::new))
        // intermediate output
        .peek(arr -> System.out.println(Arrays.deepToString(arr)))
        // reduce stream of 2d arrays to a single 2d array
        .reduce((arr1, arr2) -> Arrays.stream(arr1)
                // combinations of inner arrays
                .flatMap(inner1 -> Arrays.stream(arr2)
                        // merge two inner arrays into one
                        .map(inner2 -> Stream.of(inner1, inner2)
                                .flatMapToInt(Arrays::stream)
                                .toArray()))
                // array of combinations
                .toArray(int[][]::new))
        // otherwise an empty 2d array
        .orElse(new int[0][]);
// final output
System.out.println("Number of combinations: " + combinations.length);
// first 50 combinations by columns
int rows = 10, limit = 50;
IntStream.range(0, rows).forEach(i -> System.out.println(
        IntStream.range(0, limit).filter(j -> j % rows == i)
                .mapToObj(j -> Arrays.stream(combinations[j])
                        .mapToObj(String::valueOf)
                        .collect(Collectors.joining()))
                .collect(Collectors.joining(" "))));

Intermediate output:

[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]

Final output, first 50 combinations by columns:

Number of combinations: 9765625
1111111111 1111111131 1111111151 1111111221 1111111241
1111111112 1111111132 1111111152 1111111222 1111111242
1111111113 1111111133 1111111153 1111111223 1111111243
1111111114 1111111134 1111111154 1111111224 1111111244
1111111115 1111111135 1111111155 1111111225 1111111245
1111111121 1111111141 1111111211 1111111231 1111111251
1111111122 1111111142 1111111212 1111111232 1111111252
1111111123 1111111143 1111111213 1111111233 1111111253
1111111124 1111111144 1111111214 1111111234 1111111254
1111111125 1111111145 1111111215 1111111235 1111111255

See also: How do I generate combinations of two arrays?