Permutations algorithm

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Permutations algorithm

Post by gyroscope » Thu Feb 05, 2009 5:01 pm

"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!

:)
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

Tim
Posts: 29
Joined: Wed Oct 31, 2007 12:56 pm

Post by Tim » Thu Feb 05, 2009 6:46 pm

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.

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 6:59 pm

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.

:)
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5132
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 05, 2009 8:03 pm

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
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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 8:37 pm

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.

:)
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 9:09 pm

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!!) :wink:
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5132
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 05, 2009 9:46 pm

Hi gyroscope,

Could you post your entire script, please?

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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 10:04 pm

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
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5132
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Feb 05, 2009 10:16 pm

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
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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 10:23 pm

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.... :?
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 10:30 pm

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.

:)
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

malte
Posts: 1098
Joined: Thu Feb 23, 2006 8:34 pm
Location: Ostenfeld germany
Contact:

Post by malte » Thu Feb 05, 2009 10:31 pm

Hi

num is a reserved keyword in rev. synonym for number.

as in put the num of chars of "abc"

Cheers,

Malte

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 400
Joined: Tue Jan 08, 2008 3:44 pm
Location: Kent, UK
Contact:

Post by gyroscope » Thu Feb 05, 2009 10:32 pm

Thanks Malte, I had a minute ahead of you there!!

:)
-------------------------------
http://davidgriffinauthor.co.uk
http://davidgjourney.blogspot.com/

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”