Reputation: 1879
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++;
}
}
Upvotes: 0
Views: 2826
Reputation: 36703
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());
}
Upvotes: 0
Reputation: 2515
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);
}
Upvotes: 1