diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2023-03-21 19:29:19 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2023-03-21 19:29:19 +0000 |
| commit | c3cd1a0713c38f646f779bbd863880c4a1ee94bc (patch) | |
| tree | 835a45788612388717ddf1f864e6700fa739d9e2 | |
| parent | 9c5cd2108a8f6cf8b793c28051fdf8d767a4c8a9 (diff) | |
| download | perlweeklychallenge-club-c3cd1a0713c38f646f779bbd863880c4a1ee94bc.tar.gz perlweeklychallenge-club-c3cd1a0713c38f646f779bbd863880c4a1ee94bc.tar.bz2 perlweeklychallenge-club-c3cd1a0713c38f646f779bbd863880c4a1ee94bc.zip | |
w209 - Task 1 & 2
| -rwxr-xr-x | challenge-209/perlboy1967/ch1.pl | 42 | ||||
| -rwxr-xr-x | challenge-209/perlboy1967/ch2.pl | 77 |
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-209/perlboy1967/ch1.pl b/challenge-209/perlboy1967/ch1.pl new file mode 100755 index 0000000000..f46b3b1204 --- /dev/null +++ b/challenge-209/perlboy1967/ch1.pl @@ -0,0 +1,42 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 209 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-209 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Special Bit Characters +Submitted by: Mohammad S Anwar + +You are given an array of binary bits that ends with 0. + +Valid sequences in the bit string are: + +[0] -decodes-to-> "a" +[1, 0] -> "b" +[1, 1] -> "c" + +Write a script to print 1 if the last character is an “a” otherwise print 0. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +sub isSpecialBitCharacter { + return 0 if $_[-1]; + return (split /(11|10)/,join '',@_ )[-1] ? 0 : 1; +} + +is(isSpecialBitCharacter(1,0,0),1); +is(isSpecialBitCharacter(1,1,1,0),0); +is(isSpecialBitCharacter(1,0,0,1,1,0),1); +is(isSpecialBitCharacter(1,1,0,0,0,1,0),0); +is(isSpecialBitCharacter(1,0,1,0,1,1,0),1); + +done_testing; diff --git a/challenge-209/perlboy1967/ch2.pl b/challenge-209/perlboy1967/ch2.pl new file mode 100755 index 0000000000..ece03dbfa5 --- /dev/null +++ b/challenge-209/perlboy1967/ch2.pl @@ -0,0 +1,77 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 209 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-209 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Merge Account +Submitted by: Mohammad S Anwar + +You are given an array of accounts i.e. name with list of email addresses. + +Write a script to merge the accounts where possible. The accounts can only +be merged if they have at least one email address in common. + +=cut + +use v5.16; + +use common::sense; + +use List::MoreUtils qw(uniq duplicates); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +sub mergeAccount { + + # Change all e-mail addresses to lowercase + @_ = map {[shift @$_,map {lc} @$_]} @_; + + my @a = shift; + + while (my $r = shift) { + my ($b,@e) = @$r; + my $u = 1; + for my $i (0 .. scalar @a - 1) { + my ($c,@c) = @{$a[$i]}; + if (duplicates(@c,@e)) { + push(@{$a[$i]},@e); $u = 0; last; + } + } + push(@a,[$b,@e]) if $u; + } + + return map {[shift @$_,uniq sort @$_]} + sort { $$a[0] cmp $$b[0] } @a; +} + +cmp_deeply( + [mergeAccount(@{[ + [qw(A A1@a.com a2@a.com)], + [qw(B B1@b.com)], + [qw(A a3@a.com a1@a.com)], + ]})], + [ + [qw(A a1@a.com a2@a.com a3@a.com)], + [qw(B b1@b.com)], + ] +); +cmp_deeply( + [mergeAccount(@{[ + [qw(A a1@a.com a2@a.com)], + [qw(B b1@b.com)], + [qw(A a3@a.com)], + [qw(B b2@b.com b1@b.com)], + ]})], + [ + [qw(A a1@a.com a2@a.com)], + [qw(A a3@a.com)], + [qw(B b1@b.com b2@b.com)], + ] +); + +done_testing; |
