Permutations algorithm
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Livecode Opensource Backer
- Posts: 404
- Joined: Tue Jan 08, 2008 3:44 pm
- Location: Kent, UK
- Contact:
Permutations algorithm
"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!
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!
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
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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- Livecode Opensource Backer
- Posts: 404
- Joined: Tue Jan 08, 2008 3:44 pm
- Location: Kent, UK
- Contact:
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!!)
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!!)
-
- Livecode Opensource Backer
- Posts: 404
- Joined: Tue Jan 08, 2008 3:44 pm
- Location: Kent, UK
- Contact:
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
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:
and all permuations of 123456 will end up in the message box. You could also put it into a field, of course.
Best,
Mark
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
Best,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
-
- Livecode Opensource Backer
- Posts: 404
- Joined: Tue Jan 08, 2008 3:44 pm
- Location: Kent, UK
- Contact:
Thanks Mark, I guess I could put:
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....
Code: Select all
on mouseUp
put permutations("123456") into field "FieldA"
end mouseUp
Object: button "Button"
Line: function factorial num
Hint: num
So it doesn't seem to like "num" in Rev, for some reason....