#!/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"}+\$wives{"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(@_); }