diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-10-13 17:51:11 -0500 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-10-13 17:51:11 -0500 |
| commit | 07a79819423e01b25d3c2e8e2912a686268d6ed5 (patch) | |
| tree | 619d18a7b0fa7d1705d9e970a060d55b8ef0da58 | |
| parent | 682d4c758b46d1c5a1e27754a3beb4ea784cb770 (diff) | |
| download | perlweeklychallenge-club-07a79819423e01b25d3c2e8e2912a686268d6ed5.tar.gz perlweeklychallenge-club-07a79819423e01b25d3c2e8e2912a686268d6ed5.tar.bz2 perlweeklychallenge-club-07a79819423e01b25d3c2e8e2912a686268d6ed5.zip | |
Task 2 handle ties better
| -rw-r--r-- | challenge-343/bob-lied/perl/ch-2.pl | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/challenge-343/bob-lied/perl/ch-2.pl b/challenge-343/bob-lied/perl/ch-2.pl index fc413251b4..bcc09fd298 100644 --- a/challenge-343/bob-lied/perl/ch-2.pl +++ b/challenge-343/bob-lied/perl/ch-2.pl @@ -100,20 +100,10 @@ sub champion(@grid) my @strongest = indexes { $_ == $maxStrength } @strength; # Eliminate losers - # - #my @champ; - #for my $team1 ( @strongest ) - #{ - # next if any { $grid[$_][$team1] } @strongest ; - # push @champ, $team1; - #} - # - - # For a possible tie, choose the smallest - #my @champ = grep { my $t = $_; ! any { $grid[$_][$t]} @strongest } @strongest; - #return $champ[0]; - - return (grep { my $t = $_; ! any { $grid[$_][$t]} @strongest } @strongest)[0]; + my @champ = grep { my $t = $_; ! any { $grid[$_][$t]} @strongest } @strongest; + + # For a possible tie, return a list. If no champions, it's an N-way tie. + return ( @champ > 0 ? "@champ" : "@strongest" ); } sub runTest @@ -136,11 +126,11 @@ sub runTest { case => "Example 5", expect => 2, input => [ [0,0,0,0,0], [1,0,0,0,0], [1,1,0,1,1], [1,1,0,0,0], [1,1,0,1,0] ] } , - { case => "Rock paper scissors", expect => undef, + { case => "Rock paper scissors", expect => "0 1 2", input => [ [0,0,1], [1,0,0], [0,1,0] ] } , - { case => "Tied ", expect => 1, - input => [ [0,0,0,0,0,0], [0,0,0,0,1,1], [0,0,0,0,0,0], [0,0,1,1,0,0], [0,0,0,0,0], [0,0,0,0,0,0] ] + { case => "Tied ", expect => "1 3", + input => [ [0,0,0,0,0,0], [0,0,0,0,1,1], [0,0,0,0,0,0], [1,0,1,0,0,0], [0,0,0,0,0], [0,0,0,0,0,0] ] } , ); |
