diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-02 02:14:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-02 02:14:10 +0100 |
| commit | cf567a97b7cec84f339bbc2a6930164206e5783b (patch) | |
| tree | 5e0b657375fad5467a6a3d9f63dc5b17ba1e1561 | |
| parent | 74a5c3e56bf2b7860fc760be32e145516b9c0766 (diff) | |
| parent | 0f5bdfb7781cc28c490df9f527e86e8515750985 (diff) | |
| download | perlweeklychallenge-club-cf567a97b7cec84f339bbc2a6930164206e5783b.tar.gz perlweeklychallenge-club-cf567a97b7cec84f339bbc2a6930164206e5783b.tar.bz2 perlweeklychallenge-club-cf567a97b7cec84f339bbc2a6930164206e5783b.zip | |
Merge pull request #1784 from jacoby/master
I guess I thought I PRd this previously
| -rwxr-xr-x | challenge-062/dave-jacoby/perl/addresses.txt | 5 | ||||
| -rwxr-xr-x | challenge-062/dave-jacoby/perl/ch-1.pl | 59 | ||||
| -rwxr-xr-x | challenge-063/dave-jacoby/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-063/dave-jacoby/perl/ch-2.pl | 34 |
4 files changed, 128 insertions, 0 deletions
diff --git a/challenge-062/dave-jacoby/perl/addresses.txt b/challenge-062/dave-jacoby/perl/addresses.txt new file mode 100755 index 0000000000..9922f450db --- /dev/null +++ b/challenge-062/dave-jacoby/perl/addresses.txt @@ -0,0 +1,5 @@ +name@example.org +rjt@cpan.org +Name@example.org +rjt@CPAN.org +user@alpha.example.org
\ No newline at end of file diff --git a/challenge-062/dave-jacoby/perl/ch-1.pl b/challenge-062/dave-jacoby/perl/ch-1.pl new file mode 100755 index 0000000000..fa2ceb983c --- /dev/null +++ b/challenge-062/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/env perl + +# USAGE: ch-1.pl [-u] [file ...] + +use strict; +use warnings; +use utf8; +use feature qw{ fc postderef say signatures state switch }; +no warnings qw{ experimental }; + +use Getopt::Long; +use Carp; +use JSON; + +my $json = JSON->new->canonical->allow_nonref->pretty->space_after; +my $data = []; +my $unique = 0; +GetOptions( unique => \$unique ); + +if ( scalar @ARGV ) { + for my $file (@ARGV) { + if ( -f $file && open my $fh, '<', $file ) { + + # here might be the place for a looks_like_an_email + # test, but oh, well... + my @x = map { chomp $_; $_ } <$fh>; + push $data->@*, @x; + } + } +} +else { + $data->@* = map { chomp $_; $_ } <DATA>; +} + +my @sorted = sort_email_addresses( $data, $unique ); +say join "\n", @sorted; + +sub sort_email_addresses ( $data, $unique = 0 ) { + my $hash = {}; + my @output = + + map { $_->[0] } + grep { !$unique || $hash->{ $_->[3] }++ < 1 } + sort { fc $a->[2] cmp fc $b->[2] } + sort { $a->[1] cmp $b->[1] } + map { $_->[3] = join '@', $_->[1], fc $_->[2]; $_ } + map { [ $_, split /\@/, $_ ] } + map { chomp $_; $_ } + + $data->@*; + return wantarray ? @output : \@output; +} + +__DATA__ +name@example.org +rjt@cpan.org +Name@example.org +rjt@CPAN.org +user@alpha.example.org diff --git a/challenge-063/dave-jacoby/perl/ch-1.pl b/challenge-063/dave-jacoby/perl/ch-1.pl new file mode 100755 index 0000000000..be3a29e8e1 --- /dev/null +++ b/challenge-063/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +# USAGE: ch-1.pl [-u] [file ...] + +use strict; +use warnings; +use utf8; +use feature qw{ fc postderef say signatures state switch }; +no warnings qw{ experimental }; + +use Getopt::Long; +use Carp; +use JSON; + +my $json = JSON->new->canonical->allow_nonref->pretty->space_after; + +say last_word(); +say last_word( ' hello world', qr/[ea]l/ ) || 'undef'; # 'hello' +say last_word( "Don't match too much, Chet!", qr/ch.t/i ) || 'undef'; # 'Chet!' +say last_word( "spaces in regexp won't match", qr/in re/ ) || 'undef'; # undef +say last_word( join( ' ', 1 .. 1e6 ), qr/^(3.*?){3}/ ) || 'undef'; # '399933' + +sub last_word ( $string = ' strang string', $regex = qr/\w/ ) { + my ($output) = + reverse + grep { /$regex/ } + grep { /\S/ } + split /\s+/, $string; + return $output; +} diff --git a/challenge-063/dave-jacoby/perl/ch-2.pl b/challenge-063/dave-jacoby/perl/ch-2.pl new file mode 100755 index 0000000000..c4413d7d29 --- /dev/null +++ b/challenge-063/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use utf8; +use feature qw{ postderef say signatures state switch }; +no warnings qw{ experimental }; + +use Carp; +use JSON; +my $json = JSON->new->canonical->allow_nonref->pretty->space_after; + +my $word = 'xyxx'; + +say rotate_string($word); + +sub rotate_string( $word ) { + say $word; + my $l = length $word; + my $c = 0; + my $copy = $word; + while (1) { + my $m = $c % $l; + $c++; + my @copy = split //, $copy; + for ( 0 .. $m ) { + push @copy, shift @copy; + } + $copy = join '', @copy; + return $c if $word eq $copy; + exit if $c > 10; + } + return 1; +} |
