Little maths question

Alex Tweedly alex at tweedly.net
Mon Sep 12 15:12:42 EDT 2005


Mark Smith wrote:

> This is probably really easy, but I'm so stupid when it comes to maths 
> that I can't figure it out:
>
> Given a list of any number of lines, how can I generate a list of 
> every possible combination of those lines ie.
>
Here's a way to generate all combinations of the numbers - you can go 
from there to lines (or whatever) ...

> function combinations pNum
>   local tRes, temp, L
>   if pNum <= 1 then
>     return 1 & cr
>   end if
>   put  combinations(pNum-1) into temp
>   repeat for each line L in temp
>     put L & cr after tRes
>   end repeat
>   put pNum & cr after tRes
>   repeat for each line L in temp
>     put L & comma & pNum & cr after tRes
>   end repeat
>   return tRes
> end combinations
>
or you can do it slightly more tersely and efficiently, but in a less 
attractive order (*) as

> function combinations pNum
>   local tRes, temp, L
>   if pNum <= 1 then
>     return 1 & cr
>   end if
>   put  combinations(pNum-1) into temp
>   repeat for each line L in temp
>     put L & comma & pNum & cr after tRes
>     put L & cr after tRes
>   end repeat
>   return tRes & pNum & cr
> end combinations
>

(*) "attractive" ?? I don't know why - but I find the order produced by 
the first code fragment more easy to follow, and hence more 
"attractive". Call me a geek :-).

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.10.21/96 - Release Date: 10/09/2005




More information about the use-livecode mailing list