Chromatica
Chromatica

Reputation: 123

Generating Binary Sequences

I want to generate every possible binary sequence of numbers, where each sequence in the list is limited to a specific number of 1's and there is padding of zeros to make every list the same length.

For example, if the sequence is supposed to be 4 numbers long, and have 2 ones, all sequences would be:

1100 1010 1001 0110 0101 0011

and the zeros at the front of the number are preserved.

Upvotes: 2

Views: 986

Answers (4)

hinneLinks
hinneLinks

Reputation: 3736

Try this:

//Edit your min and max, e.g. Integer.MAX_VALUE and Integer.MIN_VALUE
int min = 0;
int max = 10;

for(int i = min; i < max; i++){
    //Make 16 bit long binary Strings
    String s = String.format("%16s", Integer.toBinaryString(i)).replace(' ', '0');

    //Make 4 bits long chunks
    List<String> chunks = new ArrayList<>();
    Matcher matcher = Pattern.compile(".{0,4}").matcher(s);
    while (matcher.find()) {
        chunks.add(s.substring(matcher.start(), matcher.end()));
    }

    StringBuilder b = new StringBuilder();
    for (String c : chunks) {
        //Here you can count the 1 and 0 of the current chunk with c.charAt(index)
        b.append(c);
        b.append(" ");
    }
    System.out.println(b.toString());
}

Upvotes: 1

f_puras
f_puras

Reputation: 2505

Requires org.apache.commons.lang.StringUtils, but this makes it a short one:

final int digits = 4;
final int onesrequired = 2;

int maxindex = (int) Math.pow(2, digits);

for (int i = 0; i < maxindex; i++) {
    String binaryStr = Integer.toBinaryString(i);

    if (StringUtils.countMatches(binaryStr, "1") == onesrequired) {
        System.out.print(String.format("%" + digits + "s", binaryStr).replace(' ', '0') + " ");
    }
}

Upvotes: 1

dosw
dosw

Reputation: 431

This can be solved using recursive function calls:

public class BinarySequences {

    public static void main(String[] args) {

        final int numCount = 4; 
        final int oneCount = 2; 

        checkSubString(numCount, oneCount, "");

        for (String res : results) {
            System.out.println(res);
        }
    }

    private static List<String> results = new ArrayList<>();

    private static void checkSubString(int numCount, int oneCount, String prefix) {
        if ((numCount >= oneCount) && (oneCount >= 0)) {
            if (numCount==1) {
                if (oneCount==1) {
                    results.add(prefix + "1");
                } else {
                    results.add(prefix + "0");
                }
            } else {
                checkSubString(numCount-1, oneCount  , prefix + "0");
                checkSubString(numCount-1, oneCount-1, prefix + "1");
            }
        }
    }


} 

Upvotes: 3

Sven Olderaan
Sven Olderaan

Reputation: 70

If you want to preserve the 0s, then just add padding:

int end = 100; //Change this
for (int i = 0; i <= end; i++) {
    String bytestring = Integer.toBinaryString(i);
    String padding = "00000000000000000000000000000000";
    bytestring = padding.substring(0, 32 - bytestring.length()) + bytestring;
    System.out.println(bytestring);
}

Upvotes: 2

Related Questions