in reply to Passing a complex array to a function

I hesitated to post a reply since even the poster admits it looks like homework. However my curiousity took over and I decided to write a program to solve the problem.
Since it isn't homework I used an external module that does the permutations for me List::Permutor.
use strict; use List::Permutor; =head1 Stupid question A man left a legacy of $10,000 to three relatives and their + wives. Together, the wives received $3960. June received $100 more + than Camille, and Martha received $100 more than June. Jack Smit +h was given just as much as his wife, Horace Saunders got half as + much again as his wife, and Terry Conners received twice as much + as his wife. What was the first name of each man's wife? =cut # there is $300 difference between # the most and least given each wife # so we subtract the 300 and divide by for # 3 for the base point for each wife my $total_for_wives = 3960; my $am = (($total_for_wives - 300) / 3); my %wives = ( Camille => $am, June => $am + 100, Martha => $am + 200, ); my @wives_combo; my $perm = List::Permutor->new( sort keys %wives ); while (my @set = $perm->next) { push ( @wives_combo , \@set) ; } my %husbands = ( 'Terry Conners' => 2 , 'Horace Saunders' => 1.5 , 'Jack Smith' => 1 ); foreach my $array (@wives_combo) { my $total; my $count; foreach (sort { $b <=> $a } values %husbands ) { $total += ($wives{$array->[$count++]} * $_); } if ( ($total += $total_for_wives) == 10000) { my $num; foreach (sort { $husbands{$b} <=> $husbands{$a} } keys %husbands) { printf "$_ wife's name is: $array->[$num++]\n"; } last; } }
Update: Code has been replaced after a bug was spotted by Anonymous Monk.

Replies are listed 'Best First'.
Re: Re: Passing a complex array to a function
by Anonymous Monk on Jan 26, 2002 at 11:05 UTC

    trs++ to you on this answer. I was heading down a long and sloppy road with my attempt.I'm sure that this was probably someone's homework at some point :-) If only I could be so lucky as to have a good Perl course availible. Otherwise I learn from books, at my job, and from silly practices like this.(and of couse from staying tuned to perlmonks)

    One Question, This looked like a possible typo, but I haven't tried to use your code yet.
    'Jack Smith' => 0 # same amount as his wife
    Should that be a 1?
    japhy I'm truly impressed. ...and a little frightened.
      Thanks for pointing that out. I had a conditional when I didn't need one because I forgot I could * by 1 and get the same value. DOH!
      The code in the initial post I made has been updated to the working version.