This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Bruteforcer with desending order of char[]

Ryan Miller
Greenhorn
Posts: 3
Been trying to figure this out for days now and could really use to help. Basically I have a bruteforcing algorithm and instead of it generating from 00000 - zzzzzz I want to go from zzzzzz to 00000. The method I have in my bruteforcer uses binary search which requires any char[] to be sorted. I found something on google saying I could achieve the reversal I want by using the reverseOrder() method within the Collection class and I've been trying every possible thing I can but can not get the results I want. Would anyone happen to have any ideas? If not, what would a good way to do this without the binary search be? Just a linkedlist? Thank you!

Steve Luke
Bartender
Posts: 4181
22
So what is it doing wrong? Be specific about what you want, what it does, and what the difference is.

Ryan Miller
Greenhorn
Posts: 3
The issue is in the increment method. The increment method cycles through every combination of the chars in the array. Say I pass my char[] and a length of 5 through the constructor, it will start at 00000 and will keep going on forever. When I pass my char[], every char must be in ascending order and cannot be mixed do to the nature of the binarySearch(). (It must be 1 2 3 4 and cannot be 2 4 1 3). What I'm trying to do is instead of generating my combinations in ascending order, I want to do them in descending order. i.e. I pass through my char[] and a length of 5 through the constructor and it starts at zzzzz and descends all the way down to 00000. I'm trying to find a way to do this without conflicting with the nature of binarySearch.

Basically I want to count down instead of count up but binarySearch is preventing it because the array passed must be in order i.e.:

char[] charset = {"0", "1", "2"};

and can not pass:

char[] charset = {"2", "1", "0"};

Jeff Verdegan
Bartender
Posts: 6109
6
Ryan Miller wrote:

Basically I want to count down instead of count up but binarySearch is preventing it because the array passed must be in order i.e.:

char[] charset = {"0", "1", "2"};

and can not pass:

char[] charset = {"2", "1", "0"};

If if you're given {0, 1, 2}, and you can't change that, but you want {2, 1, 0}, then simply reverse the order in which you iterate over the array.

The idiom is the most common way to iterate an array, but it's not The Sacred High Writ Of All That Is Holy. The stuff inside the parens is not written in stone. Think about what those pieces actually mean, and how you could change them to iterate in the other direction.

Winston Gutkowski
Bartender
Posts: 10527
64
Ryan Miller wrote:The issue is in the increment method.

It may well be, but I'd say you're concentrating far too much on implementation. What you (and we) need to understand is what you want to do; and you DON'T do that by writing code.

Coding is the final result of a lot of thinking, and it's basically a mechanical process. The important (and creative) part of programming comes in ordering your ideas and working out the design - and design is mostly about what, not how.

Being a programmer is a bit like being a writer: For a writer, most of the best words have already been said better by Shakespeare; for programmers, most algorithms (ie, code) have already been written, so the art comes in constructing your programs.

My advice: concentrate on what needs to be done, not how you're going to do it. When you have a good grasp of that, the 'how' will come almost automatically.

Winston