Permutation function
I read an e-mail about somebody looking for permutations.. Sorry, I'm a
bit short on time so I won't have a chance to look for that e-mail or
test out the code thoroughly (though my quick test shows that it works).
Here is a little function that I have been using to find permutations.
You call it with the set of characters you want to be used (ex: {'A',
'B', 'C'} or {'1', '2', '3'}) and the length of each permutation (ex: 3,
6, 9).
It's not the most optimized function in the world, but so far I haven't
had any problems. I was recently working with the code, so it may not
work exactly right (sorry!). I thought if I sent it to the list I could
get some c&c on it. Tell me what you think!
~Tom
-- code --
function GetPermutations( sequence characters, atom len )
sequence final, combo
integer chars
atom doit doit = 1
final = {}
combo = repeat(1, len)
chars = length(characters)
while doit do
final = append(final, combo)
combo[1] += 1
for i = 1 to len do
if combo[i] > chars then
if i = len then
combo[i] = chars
final = append(final, combo)
doit = 0
exit
else
combo[i..i+1] = {1, combo[i+1]+1}
end if
end if
end for
end while
for i = 1 to length(final) do
for p = 1 to length(final[i]) do
final[i][p] = characters[final[i][p]]
end for
end for
return final
end function
-- code --
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.410 / Virus Database: 231 - Release Date: 10/31/2002
|
Not Categorized, Please Help
|
|