Re[2]: How to make routine like this

new topic     » topic index » view thread      » older message » newer message

Thanks!

But are there better solutions, without checking duplicate
digits in in the left? Because I need it to be very fast.

_____________________________________________________________________

e> <aku at inbox.as> wrote:

>> input       output (can be any length)
>> (parameter)
>>
>>     1       {0,1,2,3,4}
>>     2       {0,1,2,3,5}
>>     3       {0,1,2,3,6}
>>     .
>>     .
>>     .
>>   x-2       {9,8,7,5,4}
>>   x-1       {9,8,7,5,6}
>>     x       {9,8,7,6,5}
>>
>>
>> How to make such routine?
>> And how to determine x?
>>
>> Thanks very much!

e> Reading from left to right in the sequence, you have a choice of 10 digits
e> in the first position, nine in the second (since you're not repeating digits
e> and you've used one already in the first column), eight in the third, seven
e> in the fourth, and six in the fifth. Thats 10x9x8x7x6 possible combinations,
e> or 30240.

e> It's similar to work it out for any length. Notice it wouldn't be possible
e> to have a length more than 10 - you don't have enough symbols.

e> Here's one way to do it (untested). It won't work if your data columns
e> require numbers outside 0-9 though...
e> --------
e> sequence numbers, format
e> integer len, i, invalid, skip

e> len = 5 -- set length here
e> format = sprintf("%%0%dd", len)

e> skip = 0
e> for j = 1 to len-1 do
e>     skip = 10 * skip + j
e> end for

e> i = skip
e> while i < power(10, len+1) - skip do
e>     invalid = 1
e>     while invalid do
e>         numbers = sprintf(format,i) - '0'
e>         invalid = 0
e>         for j = 2 to 5 do
e>             if find(numbers[j-1], numbers[j..5]) then
e>                 invalid = 1
e>                 i += 1
e>                 exit
e>             end if
e>         end for
e>     end while
e>     ? numbers
e>     i += 1
e> end while
e> --------
e> There are ways to adapt this to accept any range of data items, or even any
e> *set* of data items. These are left as exercises to the reader. Hint: don't
e> use sprintf(). :)

e> HTH,
e> Carl

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu