Steve Wampler wrote: > Steve Wampler wrote: > >>Steve Graham wrote: >> >> >>>I'm looking for a way to generate every possible combination of n letters >>>out of a total of m letters. >>> >>>For example, if the available letters are A, B, C and I want to generate >>>all possible 2-letter combinations, it should generate the following: >>>AB >>>BA >>>AC >>>CA >>>BC >>>CB >>> >>>I think I've seen a trivial way of doing this in Icon/Unicon but don't >>>remember. Any ideas? >> >> >>Hi Steve, >> >>There's a method comp(s, n) in the IPL (procs/strings.icn) that does what you >>want. The >>code is: >> >> procedure comb(s, i) #: character combinations >> local j >> >> if i < 1 then fail >> suspend if i = 1 then !s >> else s[j := 1 to *s - i + 1] || comb(s[j + 1:0], i - 1) >> >> end > > > Actually, in reading your mail again, I think you mean 'permutations', not > combinations. The following code should work: > > procedure permute(s, i) > local j > > if i < 1 then fail > suspend if i = 1 then !s > else s[j := 1 to *s] || permute(s[1+:j-1]||s[j+1:0], i-1) > end
And finally, here's another approach. Note that the semantics are slightly different, in that this version starts by removing duplicated letters from the input: procedure permute(s, i) if i < 1 then fail s := cset(s) suspend if i = 1 then !s else (c := !s) || permute(s--c, i-1) end -- Steve Wampler -- [EMAIL PROTECTED] The gods that smiled on your birth are now laughing out loud. ------------------------------------------------------- This SF.Net email is sponsored by the JBoss Inc. Get Certified Today * Register for a JBoss Training Course Free Certification Exam for All Training Attendees Through End of 2005 Visit http://www.jboss.com/services/certification for more information _______________________________________________ Unicon-group mailing list Unicon-group@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/unicon-group