Re: Permuting beyond one collection
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Tue, 20 Nov 2007 20:58:03 -0600
jehugaleahsa@xxxxxxxxx wrote:
This program will take phone numbers, excluding 0 and 1 and generate
all the possible words that could be made from the inputted numbers
(of any length). I do it now by appending all the current column's
letters to the fronts of all the existing substrings. This way I am
building my words from back to front. However, I know this could be a
much better solution if I could generate full-words and process them
as I go.
Ah, you want to generate all possible permutations, but you don't want
to store the entire list as an intermediate value. You just want to
generate one, then move on to the next.
Luckily, there's a fairly easy way to do it. (Actually, there are
two fairly easy ways, but I'll stick to the more obvious one.)
Basically, for each position in the output string, you have an
array which is a list of choices. What you can do is create another
array, this one an array of integers, and in that array, store indexes
into the other array.
Start those indexes all at 0. That gives you one of the permutations:
you just take the 0th item in every list. Then add 1 to the index
at the last position of the index array. Now you have another
permutation, because you've changed the last character of the output
string.
Continue in this manner until you get an index that would be out of
range. When that happens, reset that index to 0 and add one to
the next index over. (And if it causes that index to be out of
range for its list, reset it to 0 and add one to the next index
after that, and so on.)
This process is much like adding 1 to a number: you usually just
increment the last digit, but if the last digit is a 9, you have to
set it to 0 and then increment the next digit to the left instead.
And that digit might be a 9 as well. The difference here is that
you don't stop incrementing at 9; you stop incrementing an index
when you've run out of items to look at (in that column).
- Logan
.
- References:
- Permuting beyond one collection
- From: jehugaleahsa@xxxxxxxxx
- Permuting beyond one collection
- Prev by Date: Permuting beyond one collection
- Next by Date: Re: Places to purchase PowerBASIC?
- Previous by thread: Permuting beyond one collection
- Next by thread: Re: Permuting beyond one collection
- Index(es):
Relevant Pages
|