diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-11 22:09:46 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-11 22:09:46 +0100 |
| commit | dcc636a86c6cb05cffbaa330d3fe093ac898d74e (patch) | |
| tree | f69c4d9668980d437281eed9ea0d4ecd747ab2df /challenge-055 | |
| parent | a1552b3b2ce9fbb2a518fb203326c09cf529515a (diff) | |
| download | perlweeklychallenge-club-dcc636a86c6cb05cffbaa330d3fe093ac898d74e.tar.gz perlweeklychallenge-club-dcc636a86c6cb05cffbaa330d3fe093ac898d74e.tar.bz2 perlweeklychallenge-club-dcc636a86c6cb05cffbaa330d3fe093ac898d74e.zip | |
- Fixed flip binary solutions when more than one result is expected.
Diffstat (limited to 'challenge-055')
| -rw-r--r-- | challenge-055/mohammad-anwar/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-055/mohammad-anwar/perl/ch-1a.pl | 26 |
2 files changed, 44 insertions, 4 deletions
diff --git a/challenge-055/mohammad-anwar/perl/ch-1.pl b/challenge-055/mohammad-anwar/perl/ch-1.pl index bce2c5008c..698f8ec222 100644 --- a/challenge-055/mohammad-anwar/perl/ch-1.pl +++ b/challenge-055/mohammad-anwar/perl/ch-1.pl @@ -34,5 +34,25 @@ sub flip_binary { } } - return [ sort { $result->{$b} <=> $result->{$a} } keys %$result ]->[0]; + return flipped_binary($result); +} + +sub flipped_binary { + my ($result) = @_; + + my $v; + my @r; + foreach my $k (sort { $result->{$b} <=> $result->{$a} } sort keys %$result) { + if (defined $v) { + if ($result->{$k} == $v) { + push @r, $k; + } + } + else { + $v = $result->{$k}; + push @r, $k; + } + } + + return join (" | ", @r); } diff --git a/challenge-055/mohammad-anwar/perl/ch-1a.pl b/challenge-055/mohammad-anwar/perl/ch-1a.pl index 55cf2a0c51..3366aaadff 100644 --- a/challenge-055/mohammad-anwar/perl/ch-1a.pl +++ b/challenge-055/mohammad-anwar/perl/ch-1a.pl @@ -6,8 +6,8 @@ use warnings; use Test::More; is (flip_binary("10001"), "11111 (2,4)"); -is (flip_binary("10101"), "11011 (2,4)"); -is (flip_binary("00101"), "11011 (1,4)"); +is (flip_binary("10101"), "10111 (4,4) | 11011 (2,4) | 11101 (2,2)"); +is (flip_binary("00101"), "11011 (1,4) | 11101 (1,2)"); done_testing; @@ -38,5 +38,25 @@ sub flip_binary { } } - return [ sort { $result->{$b} <=> $result->{$a} } keys %$result ]->[0]; + return flipped_binary($result); +} + +sub flipped_binary { + my ($result) = @_; + + my $v; + my @r; + foreach my $k (sort { $result->{$b} <=> $result->{$a} } sort keys %$result) { + if (defined $v) { + if ($result->{$k} == $v) { + push @r, $k; + } + } + else { + $v = $result->{$k}; + push @r, $k; + } + } + + return join (" | ", @r); } |
