diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-26 21:22:10 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-05-26 21:22:10 +0100 |
| commit | b6f9cf067774675ef18c7ea376963f943a531624 (patch) | |
| tree | a1add87717f3a41854128cf5c3b4b96f8d52593c /challenge-062/mohammad-anwar | |
| parent | 714a94d78dc2ba032793a2a51cab0a67d78cc780 (diff) | |
| download | perlweeklychallenge-club-b6f9cf067774675ef18c7ea376963f943a531624.tar.gz perlweeklychallenge-club-b6f9cf067774675ef18c7ea376963f943a531624.tar.bz2 perlweeklychallenge-club-b6f9cf067774675ef18c7ea376963f943a531624.zip | |
- Added Raku solutions to the "Sort Email Addresses" task.
Diffstat (limited to 'challenge-062/mohammad-anwar')
| -rw-r--r-- | challenge-062/mohammad-anwar/raku/ch-1.p6 | 42 | ||||
| -rw-r--r-- | challenge-062/mohammad-anwar/raku/ch-1a.p6 | 55 |
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-062/mohammad-anwar/raku/ch-1.p6 b/challenge-062/mohammad-anwar/raku/ch-1.p6 new file mode 100644 index 0000000000..f79c2ded4d --- /dev/null +++ b/challenge-062/mohammad-anwar/raku/ch-1.p6 @@ -0,0 +1,42 @@ +#!/usr/bin/env perl6 + +use v6.d; + +sub MAIN(Str :$file, Bool :$unique?) { + + my $sorted = sort-data($file, $unique); + say $sorted.join("\n"); +} + +sub sort-data(Str $file, Bool $unique?) { + + my $io := $file.IO; + die "ERROR: '$file' not found" unless $io.e; + die "ERROR: '$file' not readable" unless $io.r; + + my @source = (); + my %source = (); + for $io.lines -> Str $line { + my ($mailbox, $domain) = $line.split("@"); + push @source, [$mailbox, $domain]; + if $unique { + %source.{$mailbox} = $domain; + } + } + + my $sorted = []; + if $unique { + for %source.sort({ $^a.value.lc cmp $^b.value.lc || $^a.key cmp $^b.key }) -> $k { + my $_k = $k.keys; + my $_v = $k.values; + $sorted.push: sprintf("%s@%s", $_k, $_v); + } + } + else { + for @source.sort({ $^a.[0].lc cmp $^b.[0].lc || $^a.[1] cmp $^b.[1] }).reverse -> $k { + $sorted.push: $k.join("@"); + } + } + + return $sorted; +} diff --git a/challenge-062/mohammad-anwar/raku/ch-1a.p6 b/challenge-062/mohammad-anwar/raku/ch-1a.p6 new file mode 100644 index 0000000000..5fc2a512bf --- /dev/null +++ b/challenge-062/mohammad-anwar/raku/ch-1a.p6 @@ -0,0 +1,55 @@ +#!/usr/bin/env perl6 + +use Test; + +is-deeply sort-data('../data.txt'), + [ 'user@alpha.example.org', + 'rjt@cpan.org', + 'rjt@CPAN.org', + 'Name@example.org', + 'name@example.org', + ], + 'regular sort'; + +is-deeply sort-data('../data.txt', True), + [ 'user@alpha.example.org', + 'rjt@CPAN.org', + 'Name@example.org', + 'name@example.org', + ], + 'unique sort'; + +done-testing; + +sub sort-data(Str $file, Bool $unique?) { + + my $io := $file.IO; + die "ERROR: '$file' not found" unless $io.e; + die "ERROR: '$file' not readable" unless $io.r; + + my @source = (); + my %source = (); + for $io.lines -> Str $line { + my ($mailbox, $domain) = $line.split("@"); + push @source, [$mailbox, $domain]; + if $unique { + %source.{$mailbox} = $domain; + } + } + + my $sorted = []; + if $unique { + for %source.sort({ $^a.value.lc cmp $^b.value.lc || $^a.key cmp $^b.key }) -> $k { + my $_k = $k.keys; + my $_v = $k.values; + $sorted.push: sprintf("%s@%s", $_k, $_v); + } + } + else { + for @source.sort({ $^a.[0].lc cmp $^b.[0].lc || $^a.[1] cmp $^b.[1] }).reverse -> $k { + $sorted.push: $k.join("@"); + } + } + + return $sorted; +} |
