aboutsummaryrefslogtreecommitdiff
path: root/challenge-021
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-08-14 14:28:48 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-08-14 14:28:48 +0100
commitd165b957857c9bfd57d3a27075fe0dad2274bf2a (patch)
tree00c95da0204042c4139c47cdbe5ce9d662787c94 /challenge-021
parenta0a5bd3b7d03ab212f84dafc67252dc3d10c4267 (diff)
downloadperlweeklychallenge-club-d165b957857c9bfd57d3a27075fe0dad2274bf2a.tar.gz
perlweeklychallenge-club-d165b957857c9bfd57d3a27075fe0dad2274bf2a.tar.bz2
perlweeklychallenge-club-d165b957857c9bfd57d3a27075fe0dad2274bf2a.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-021')
-rw-r--r--challenge-021/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-021/laurent-rosenfeld/perl5/ch-1.pl18
-rw-r--r--challenge-021/laurent-rosenfeld/perl5/ch-2.pl32
-rw-r--r--challenge-021/laurent-rosenfeld/perl6/ch-1.p611
-rw-r--r--challenge-021/laurent-rosenfeld/perl6/ch-2.p632
5 files changed, 94 insertions, 0 deletions
diff --git a/challenge-021/laurent-rosenfeld/blog.txt b/challenge-021/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..8d8a381156
--- /dev/null
+++ b/challenge-021/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2019/08/perl-weekly-challenge-21-eulers-number-and-url-normalizing.html
diff --git a/challenge-021/laurent-rosenfeld/perl5/ch-1.pl b/challenge-021/laurent-rosenfeld/perl5/ch-1.pl
new file mode 100644
index 0000000000..d0b9e081ea
--- /dev/null
+++ b/challenge-021/laurent-rosenfeld/perl5/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use feature qw /say signatures/;
+no warnings 'experimental::signatures';
+
+sub fact ($n) {
+ my $fact = 1;
+ $fact *= $_ for 2..$n;
+ return $fact;
+}
+sub eul ($n) {
+ my $euler;
+ $euler += 1 / fact $_ for 0..$n;
+ return $euler;
+}
+
+say eul shift;
diff --git a/challenge-021/laurent-rosenfeld/perl5/ch-2.pl b/challenge-021/laurent-rosenfeld/perl5/ch-2.pl
new file mode 100644
index 0000000000..fb4b8baa0b
--- /dev/null
+++ b/challenge-021/laurent-rosenfeld/perl5/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use feature qw /say/;
+use Test::More tests => 5;
+
+sub normalize {
+ my $url = shift;
+ my %unreserved = map {$_ => 1} 0x41..0x5A, 0x61..0x7A, 0x2D, 0x2E, 0x5F, 0x7E;
+ for ($url) {
+ s/(\w+)/lc $1/ge; # Lowercase letters
+ s/(%\w\w)/uc $1/ge; # Capitalizing letters in escape sequences
+ # Decoding percent-encoded octets
+ if (/%([[:xdigit:]]{2})/ and exists $unreserved{hex $1} ) {
+ s/%([[:xdigit:]]{2})/chr hex "0x$1"/xge;
+ }
+ s{:80/}{/}; # Removing default port
+ s{/\.\./}{/}g; # Removing two-dots segments
+ s{/\./}{/}g; # Removing dot segments
+ }
+ return $url;
+}
+
+for ( [ 1, 'HTTP://www.Example.com/', 'http://www.example.com/' ],
+ [ 2, 'http://www.example.com/a%c2%b1b', 'http://www.example.com/a%C2%B1b' ],
+ [ 3, 'http://www.example.com/%7Eusername/', 'http://www.example.com/~username/' ],
+ [ 4, 'http://www.example.com:80/bar.html', 'http://www.example.com/bar.html' ],
+ [ 5, 'http://www.example.com/../a/../c/./d.html', 'http://www.example.com/a/c/d.html' ]
+ ) {
+ my ($num, $source, $target) = @$_;
+ is normalize($source), $target, "Test $num";
+}
diff --git a/challenge-021/laurent-rosenfeld/perl6/ch-1.p6 b/challenge-021/laurent-rosenfeld/perl6/ch-1.p6
new file mode 100644
index 0000000000..e1b0239201
--- /dev/null
+++ b/challenge-021/laurent-rosenfeld/perl6/ch-1.p6
@@ -0,0 +1,11 @@
+use v6;
+
+sub postfix:<!> (Int $n) { # factorial operator
+ [*] 2..$n;
+}
+sub eul (Int $n) {
+ [+] map { 1 / $_! }, 0..$n;
+}
+sub MAIN (Int $n) {
+ say eul $n;
+}
diff --git a/challenge-021/laurent-rosenfeld/perl6/ch-2.p6 b/challenge-021/laurent-rosenfeld/perl6/ch-2.p6
new file mode 100644
index 0000000000..a9aaab9b24
--- /dev/null
+++ b/challenge-021/laurent-rosenfeld/perl6/ch-2.p6
@@ -0,0 +1,32 @@
+use v6;
+use Test;
+
+sub normalize (Str $url is copy) {
+ constant $unreserved = (0x41..0x5A, 0x61..0x7A, 0x2D, 0x2E, 0x5F, 0x7E).Set;
+ given $url {
+ s:g/(\w+)/{lc $0}/; # Lowercase letters
+ s:g/('%'\w\w)/{uc $0}/; # Capitalizing letters in escape sequences
+ s:g/'%'(<xdigit>**2) # Decoding percent-encoded octets
+ <?{ (+"0x$0") (elem) $unreserved }> # code assertion
+ /{:16(~$0).chr}/;
+ s/':' 80 '/'/\//; # Removing default port
+ s:g/'/../'/\//; # Removing two-dots segments
+ s:g/'/./'/\//; # Removing dot segments
+ }
+ return $url;
+}
+
+plan 5;
+for < 1 HTTP://www.Example.com/
+ http://www.example.com/
+ 2 http://www.example.com/a%c2%b1b
+ http://www.example.com/a%C2%B1b
+ 3 http://www.example.com/%7Eusername/
+ http://www.example.com/~username/
+ 4 http://www.example.com:80/bar.html
+ http://www.example.com/bar.html
+ 5 http://www.example.com/../a/../c/./d.html
+ http://www.example.com/a/c/d.html
+ > -> $num, $source, $target {
+ cmp-ok normalize($source), 'eq', $target, "Test $num";
+}