Re: Permuting beyond one collection



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
.



Relevant Pages

  • Re: explanation of a program required please
    ... then ndigit[3] is incremented. ... element of the array indicated by the index i. ... You cannot increment an array. ... Each element in the array represents a digit. ...
    (comp.lang.c)
  • Re: algorithms
    ... You then increase the last digit, and if it becomes larger than the number of characters it represents, reset it and increment the next higher digit. ...
    (comp.programming)
  • Re: Auto generated values
    ... Is the 3 digit part number incremented universally? ... >> a combobox, as you mentioned. ... >> As for the increment of the number, the specifics of how to do this ... >> Steve Schapel, Microsoft Access MVP ...
    (microsoft.public.access.forms)
  • Re: clear user form after entry
    ... Sorry about any confusion these two posts may have caused... ... find the first digit that is not a zero ... problem in asking for more characters than exist, so I took a guess that ... process an array; ...
    (microsoft.public.excel.programming)
  • Re: foreach enhancement
    ... > that all variables in the array literal have the same type. ... > If it is not present in the type we have to explicity state an increment ... >> rename it to isin) as you suggested so that it doesn't emit the generator ... With lists you end up with two different sets of values added, ...
    (microsoft.public.dotnet.languages.csharp)