Page 1 of 1
Permutations algorithm
Posted: Thu Feb 05, 2009 5:01 pm
by gyroscope
"Tinkering here, tinkering there" I guess I'm not the only one to jump from project to project, and I'm working on my fourth app today (which is a major app which will take me years, for interest).
Does anyone know please, of a permutation algorithm to use in Rev scripting, which I need for this
Looking at the word permutation in Wikipedia, it seems there are a few variations of meaning.
I'm referring to the more common meaning, whereby if you have say numbers 1,2,3,4 and 5 then the algorithm would supply 13245, 13425...53241, i.e all possible combinations of the five numbers (or letters, even) as position. It would have to be self-contained in as much as it could be used with any number of letters or numbers in the set, i.e 376894 or sjyrtkjuh.
I've tried for a while to suss this but without any success; also I found C++ algorythms on the web, etc which I was unable to "translate"; I require a Basic Rev type, of course.
Any help here appreciated, thank you!

Posted: Thu Feb 05, 2009 6:46 pm
by Tim
Hi,
Have a look through the newsletter archives - one of the staff here (Laura) did an article on creating a word search/anagram program which does exactly what you want.
Best,
Tim.
Posted: Thu Feb 05, 2009 6:59 pm
by gyroscope
Thank you Tim, for your quick reply and
one of the staff here (Laura) did an article on creating a word search/anagram program
reminding me of this. Sounds just the ticket. I'm off to search for it now.

Posted: Thu Feb 05, 2009 8:03 pm
by Mark
Hi gyroscope,
Here you will find two script from the HyperCard times, which you should be able to use in Revolution without modification, but you might be able to make the scripts faster. Just enter "permutation" in the search field.
Best,
Mark
Posted: Thu Feb 05, 2009 8:37 pm
by gyroscope
Most excellent Mark, thank you very much!
I'm about to try it in Rev; I guess it will run faster than in Hypercard; I'll try lock and unlock screen as well, to see if that works in speeding it up even more.

Posted: Thu Feb 05, 2009 9:09 pm
by gyroscope
Apologies for my slow brain (I've been shying away from functions ever since I started using Rev 9 months ago):
I've put the code in between an on MouseUp and end MouseUp handler but it doesn't like that, so that can't be right; how do I call up the script from a button?
Also I'm a bit confused about where to put the actual length of string in the code.
And finally, after the line "return perms"
do I put "put permutations() into field "FieldA"" to fill the field with the results?
Any help appreciated, thanks (still a newbie, really! Perhaps another year and I'll upgrade myself!!)

Posted: Thu Feb 05, 2009 9:46 pm
by Mark
Hi gyroscope,
Could you post your entire script, please?
Best,
Mark
Posted: Thu Feb 05, 2009 10:04 pm
by gyroscope
Certainly, Mark.
Code: Select all
-- Brian Yennie
function permutations str
  put length(str) into numChoices
  put factorial(numChoices) into numP
  repeat with i=1 to numP
    put str into temp
   Â
    repeat with j=1 to numChoices-1
      put 1 into x
      put (numChoices - j + 1) into y
      repeat with k=numChoices down to y
        put x*k into x
      end repeat
      put (numP div x) into x
      put "put ( (i-1) div x) mod y + 1 into a"&j into hat
      do hat
    end repeat
    do "put 1 into a"&numChoices
   Â
    repeat with j=1 to numChoices
      do "put (char a"&j&&"of temp) after perms"
      do "delete char a"&j&&"of temp"
    end repeat
    put return after perms
   Â
  end repeat
  return perms
end permutations
function factorial num
  put 1 into x
  repeat with i=1 to num
    put x*i into x
  end repeat
  return x
end factorial
Posted: Thu Feb 05, 2009 10:16 pm
by Mark
Alright, gyroscope,
If you have the script you posted in a stack or button script, all you need is to add the following lines to your button:
Code: Select all
on mouseUp
put permutations("123456")
end mouseUp
and all permuations of 123456 will end up in the message box. You could also put it into a field, of course.
Best,
Mark
Posted: Thu Feb 05, 2009 10:23 pm
by gyroscope
Thanks Mark, I guess I could put:
Code: Select all
on mouseUp
put permutations("123456") into field "FieldA"
end mouseUp
I can't check this out because there's a compile error with the original code:
Object: button "Button"
Line: function factorial num
Hint: num
So it doesn't seem to like "num" in Rev, for some reason....

Posted: Thu Feb 05, 2009 10:30 pm
by gyroscope
Just changed the parameter to numP (used again, as it were); I guess num wasn't a keyword in Hypercard but is in Rev.
Anyhow, now works like a charm, thank you for your help, Mark.

Posted: Thu Feb 05, 2009 10:31 pm
by malte
Hi
num is a reserved keyword in rev. synonym for number.
as in put the num of chars of "abc"
Cheers,
Malte
Posted: Thu Feb 05, 2009 10:32 pm
by gyroscope
Thanks Malte, I had a minute ahead of you there!!
