0

EDIT NOTE: I've not been very clear. I'm trying to start from a single char guess such as 0 to 00 to 000 ... all the way through to zzzzzz. Basically, all possible iterations from 0 to zzzzzz. Sorry I've not been very clear!

I'm currently trying to cycle through an array of characters. The array contains 0-9 and a-z (lower case). Admittedly, this is homework - I'm a useless coder (see previous post) and I could do with a bit of help.

What I want is to iterate through all possible results of the char array and list the results...

aaa                aba
aab > through to > aca
aac                ada

If it were just based on letters I've read that I could base it on base26 number system but this includes numbers.

So far, I've managed to cycle through the array, assign the answer to a 'guess' array before cycling through at the next position. After that, I'm stumped.

Any advice, as last time, much appreciated. The work is based on Brute Force but there are plenty of working examples out there for me to use if my true aim was illegal, but it's not.

Here is what I have so far.

/**
 *
 * @author Aaron
 */
public class Test {

    /**
     * @param args the command line arguments
     */
    int current = 0;
    char[] guess = new char[6];

    public static void main(String[] args) {
        Test test = new Test();
        int maxLength = 6; 
        char c = '0';

        while (maxLength != 0) {
            maxLength--;
            test.iterateAll(c);
            test.increment(c);            
        }
    }

    public void iterateAll(char c) {
        char[] charset = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                          'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                          'u', 'v', 'w', 'x', 'y', 'z'};
        for (int i = 0; i < charset.length; i++) {
            //c = charset[i];
            guess[current] = charset[i];
            System.out.println(guess);
        }
    }

    public void increment(char c) {
        current++;
    }
}
1
  • Your idea to use base 26 is good, the fact that you also have numbers just means that you should go for base 36 instead. FauxFaux's answer seems to use that fact. Commented Mar 8, 2012 at 23:39

2 Answers 2

1

Can you use Integer.toString()? If so, it's willing to do most of the work for you. The following prints aaa, aab, aac, etc. for everything.

final int start = 36*36*10 + (36*10) + 10;
for (int i = start; i < 36*36*36; ++i) {
    final String base36 = Integer.toString(i, 36);
    final String padded = String.format("%3s", base36).replace(' ', '0');
    System.out.println(padded);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't think about base 36 but this is a good answer! Could you just explain the mathematics this example? Why is the start variable 129970? And why do we need 36*36*36? I want to be able to increment up to and including six chars so would I change that to 36*36*36*36*36*36? Thanks!
The start number is aaa, 10 represents a and 36 is the size of the alphabet; to get a series of 3s, 333, in base-10, we'd be doing (10*10*3 + 10*3 + 3)?
0

I'd use StringBuilder as lets you "manipulate" strings at character level. Here it just up, carrying values from left to right in the pos "registers", these just index the character sequence. Also note characters are just a type of number so can be used as literals in loops .

char[] seq = new char[36];
int i = 0;
for (char c = '0'; c <= '9'; c++) {
    seq[i++] = c;
}
for (char c = 'a'; c <= 'z'; c++) {
    seq[i++] = c;
}

int length = 3;
StringBuilder builder = new StringBuilder("    ");

int[] pos = new int[length];
int total = (int) Math.pow(seq.length, length);
for (int count = 0; count < total; count++) {
    for (int x = 0; x < length; x++) {
        if (pos[x] == seq.length) {
            pos[x] = 0;
            if (x + 1 < length) {
                pos[x + 1]++;
            }
        }
        builder.setCharAt(x, seq[pos[x]]);
    }
    pos[0]++;

    System.out.println(builder.toString());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.