user569685
user569685

Reputation: 191

Generate all binary sequences of different lengths and pass them to a function

I'm trying to generate all binary sequences of length 9,10 and 17 and pass them to a function.

for (int i = 0; i < states1.length; i++) {
        for (int j = 0; j < states2.length; j++) {
            for (int k = 0; k < states3.length; k++) {
                    if(func(sequences1[i],sequences2[j],sequences3[k]) == result) {
                    System.out.println("Success");
                    break;
                    }
           }
        }
    }

sequences1 would for example, hold all binary sequences of 9 lenght. Eg: 000000001,000000011...

"func" gets the binary sequences and returns a result. Basically, the issue im having is that i dont know how to generate the sequences.

I tried:

-Hard-coding them, there are too many constants.

-Reading them from files. Had problems with looping the lines.

Upvotes: 1

Views: 128

Answers (1)

zr0gravity7
zr0gravity7

Reputation: 3194

It might be difficult to hold in memory all binary strings of length 17, as there are more than 100k such strings, each 17 characters long. I assume you will be encoding the binary strings as a character array of "0" and "1" characters?

Consider implementing the sequences as a generator instance (available in Java 8+). This will make it seem as though the sequences are arrays of all possible bit strings of length n, when in reality they will be generated on the fly. This cuts down on memory usage. You can produce these generator instances with a method that takes in the length of the bit strings that will be yielded by that generator, and then returns a generator that will yield all such bit strings of that length.

I will not bother implementing the generator until you specify how the bit strings are to be encoded (as integers, strings, etc.). Please also specify if the order of the bit strings in the sequence is important, i.e. if for sequences4, 0100 must be at the index immediately after 0011.

Upvotes: 2

Related Questions