Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
This will look a lot lot like homework. But I assure you it is not. I am trying to learn how to think about Algorithims, and I sometimes do it with corny brainteaser questions.
Anyway I got the following problem off the web, and while it took me about two minutes to solve on paper using trial and error, I figured I would try it in perl in a more systematic way.
The problem creeps in after I have an array of every possible combo of man and wife. My thought was to split up the array by the square root of the total number of man-wife combos(in this case giving me three separate arrays, Where each array has one man and each possible wife.) Then to run those three arrays by reference through the original permutation sub to get references for each possible combo of Man-Wife:Man-Wife:Man-Wife which I could then process.
I had trouble passing these array slices to the permutation sub. And now I'm stuck. My work so far follows. All I really need to know is how I should do the second permutation.#!/usr/bin/perl -w use strict; use Data::Dumper; #for Debugging =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 Smith 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 #given my $legacy = 10000; my $wife_total = 3960; #postulate my %wives; $wives{"Camille"} = 0; $wives{"June"} = $wives{"Camille"} + 100; $wives{"Martha"} = $wives{"June"} + 100; my $husband_total = $legacy - $wife_total; my $wife_share = ($wife_total / 3)-($wives{"June"}+$wives{"Martha"}+$w +ives{"Camille"}); #get the amount of money for each wife for my $woman (keys %wives){ $wives{$woman} += $wife_share; } #key each husband to the multiple given in the example my %husbands = ("Jack Smith"=>1,"Horace Saunders"=>1.5,"Terry White"=> +2); my @husband_list = (keys %husbands); my @wife_list = (keys %wives); my @couples; #get every possible man and wife combo push @couples,$_ for (permute(\@husband_list, \@wife_list)); my $number_of_couples = sqrt scalar @couples; print join(",",@$_),"\n" for (@couples); =head1 2d array of husband and wife combos At this poit we have this array.... @couples =( [Horace Saunders,Martha] [Horace Saunders,June] [Horace Saunders,Camille] [Terry White,Martha] [Terry White,June] [Terry White,Camille] [Jack Smith,Martha] [Jack Smith,June] [Jack Smith,Camille] ) But what to do next? =cut #I stole this function from a perlmonks snippet from Merlyn sub permute{ my $last = pop @_; unless(@_){ return map [$_], @$last; } return map {my $left =$_; map [@$left, $_],@$last} permute(@_); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a complex array to a function
by japhy (Canon) on Jan 26, 2002 at 05:49 UTC | |
by particle (Vicar) on Jan 26, 2002 at 11:58 UTC | |
by japhy (Canon) on Jan 26, 2002 at 12:52 UTC | |
by trs80 (Priest) on Jan 26, 2002 at 11:43 UTC | |
|
Re: Passing a complex array to a function
by trs80 (Priest) on Jan 26, 2002 at 10:42 UTC | |
by Anonymous Monk on Jan 26, 2002 at 11:05 UTC | |
by trs80 (Priest) on Jan 26, 2002 at 12:10 UTC |