combination program

owner-baltimore-pm-list at pm.org owner-baltimore-pm-list at pm.org
Wed Apr 5 11:50:08 CDT 2000


This was sent to the list by Michael Clingman [Michael.D.Clingman at ssa.gov]
I just started moderating the list so I'm not sure how this is going to 
look when everyone gets it...
Rob
> This is a program that uses recursion to solve the problem brought up at
> the meeting last night.  It takes all combinations of the previous n-1
> letters and adds all combinations for the current letter.  @groups
> contains all combinations for n-1 letters and @groups_new is built to
> contain all combinations of n letters.
> 
> 
> 
> #!/usr/bin/perl
> 
> @list = ('a','b','c','d','e','f');
> 
> @result = &combine(4);
> print "@result\n";
> 
> sub combine {
>     my(@groups, at groups_new,$a,$b,$t,$n);
>     $n = shift;
>     return '' if $n == 0;
>     @groups = &combine($n-1);
>     @groups_new = @groups;
>     foreach $a (@groups) {
>         $t = $a . $list[$n-1];
>         push(@groups_new,$t);
>     }
>     return(@groups_new);
> }
> 
Sender: owner-baltimore-pm-list at pm.org
Precedence: bulk




More information about the Baltimore-pm mailing list