diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-26 11:07:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-26 11:07:21 +0100 |
| commit | 1175ab6214c92f33bfa687b7ab2fd01a4b82c50f (patch) | |
| tree | 4f1c933ea98785105fb9db02241f48852aa370f9 | |
| parent | 45c14399fa0df4831c08e7d32c46f0e4c1565d48 (diff) | |
| parent | 38563bbd53f69b4efca876a39db29c9822add18a (diff) | |
| download | perlweeklychallenge-club-1175ab6214c92f33bfa687b7ab2fd01a4b82c50f.tar.gz perlweeklychallenge-club-1175ab6214c92f33bfa687b7ab2fd01a4b82c50f.tar.bz2 perlweeklychallenge-club-1175ab6214c92f33bfa687b7ab2fd01a4b82c50f.zip | |
Merge pull request #7790 from carlos157oliveira/challenge-209
solution to challenge 209
| -rw-r--r-- | challenge-209/carlos-oliveira/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-209/carlos-oliveira/perl/ch-2.pl | 61 |
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-209/carlos-oliveira/perl/ch-1.pl b/challenge-209/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..5b2745b19f --- /dev/null +++ b/challenge-209/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use v5.36; + +use Test::More; + +sub is_last_bit_a (@bits) { + my $word = join '', @bits; + my $is_char_a = 0; + while ($word =~ /11|10|(0)/g) { + $is_char_a = defined $1; + } + return $is_char_a ? 1 : 0; +} + +is is_last_bit_a(1, 0, 0), 1; +is is_last_bit_a(1, 1, 1, 0), 0; + +done_testing; diff --git a/challenge-209/carlos-oliveira/perl/ch-2.pl b/challenge-209/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..657576dd2f --- /dev/null +++ b/challenge-209/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,61 @@ +use strict; +use warnings; +use v5.36; + +use Test::More; +use List::UtilsBy qw(partition_by sort_by); +use List::Util qw(uniqstr); + + +sub merge_accounts (@accounts) { + my @merged_accounts; + my %partitioned_accounts = partition_by { $_->[0] } @accounts; + + for my $account_name (keys %partitioned_accounts) { + my @accounts = $partitioned_accounts{$account_name}->@*; + for (my $i = 0; $i < @accounts; $i++) { + my $u = $i + 1; + while ($u < @accounts) { + my @uniq_merged_emails = uniqstr(@{$accounts[$i]}, @{$accounts[$u]}); + # Consider the name of the account, which was deduplicated above, + # between the merged emails. + if (@uniq_merged_emails + 1 == $accounts[$i]->@* + $accounts[$u]->@*) { + $u++; + next; + } + # Get rid of one of the merged entries. + # Start iterating again since the entry changed + # to be sure everything is merged together. + $accounts[$i] = \@uniq_merged_emails; + splice @accounts, $u, 1; + $u = $i + 1; + } + } + push @merged_accounts, @accounts; + } + return sort_by { $_->[0] } @merged_accounts; +} + +my @result = merge_accounts( + ['A', 'a1@a.com', 'a2@a.com'], + ['B', 'b1@b.com'], + ['A', 'a3@a.com', 'a1@a.com'] +); +is_deeply \@result, [ + ['A', 'a1@a.com', 'a2@a.com', 'a3@a.com'], + ['B', 'b1@b.com'] +]; + +@result = merge_accounts( + ['A', 'a1@a.com', 'a2@a.com'], + ['B', 'b1@b.com'], + ['A', 'a3@a.com'], + ['B', 'b2@b.com', 'b1@b.com'] +); +is_deeply \@result, [ + ['A', 'a1@a.com', 'a2@a.com'], + ['A', 'a3@a.com'], + ['B', 'b1@b.com', 'b2@b.com'] +]; + +done_testing; |
