aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-021/paulo-custodio/README1
-rw-r--r--challenge-021/paulo-custodio/perl/ch-1.pl23
-rw-r--r--challenge-021/paulo-custodio/perl/ch-2.pl53
-rw-r--r--challenge-021/paulo-custodio/test.pl33
4 files changed, 110 insertions, 0 deletions
diff --git a/challenge-021/paulo-custodio/README b/challenge-021/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-021/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-021/paulo-custodio/perl/ch-1.pl b/challenge-021/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..63d04b961f
--- /dev/null
+++ b/challenge-021/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+# Challenge 021
+#
+# Task #1
+# Write a script to calculate the value of e, also known as Euler’s number and
+# Napier’s constant. Please checkout wiki page for more information.
+
+use strict;
+use warnings;
+use 5.030;
+
+sub calc_e {
+ my $e = 1;
+ my($n, $prod, $prev) = (0, 1, 0);
+ while ($prev != $e) {
+ $prev = $e;
+ $e += 1/($prod *= ++$n);
+ }
+ return $e;
+}
+
+say calc_e();
diff --git a/challenge-021/paulo-custodio/perl/ch-2.pl b/challenge-021/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..24276dedad
--- /dev/null
+++ b/challenge-021/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+# Challenge 021
+#
+# Task #2
+# Write a script for URL normalization based on rfc3986. This task was shared by
+# Anonymous Contributor.
+#
+# According to Wikipedia, URL normalization is the process by which URLs are
+# modified and standardized in a consistent manner. The goal of the
+# normalization process is to transform a URL into a normalized URL so it is
+# possible to determine if two syntactically different URLs may be equivalent.
+
+use strict;
+use warnings;
+use 5.030;
+
+use Data::Dump 'dump';
+
+sub decode_triplets {
+ my($hex) = @_;
+ my $c = chr(hex($hex));
+ return $c if $c =~ /[a-zA-Z0-9\-._~]/;
+ return '%'.uc($hex);
+}
+
+sub norm_uri {
+ my($uri) = @_;
+ for ($uri) {
+ # Converting percent-encoded triplets to uppercase
+ s/(\%[0-9a-f]{2})/\U$1/gi;
+
+ # Converting the scheme and host to lowercase
+ s/^(\w+:\/\/)((.*?@)?)(.*?\/)/\L$1\E$2\L$4/;
+
+ # Decoding percent-encoded triplets of unreserved characters
+ s/(\%([0-9a-f]{2}))/ decode_triplets($2) /gie;
+
+ # Removing dot-segments
+ s/\/\.\//\//g;
+ s/\/[^\/]+\/\.\.\//\//g;
+
+ # Converting an empty path to a "/" path
+ s/^(\w+:\/\/[^\/]+)$/$1\//;
+
+ # Removing the default port
+ s/^(http:\/\/[^\/]+?):80\//$1\//;
+ }
+ return $uri;
+}
+
+my $uri = shift;
+say norm_uri($uri);
diff --git a/challenge-021/paulo-custodio/test.pl b/challenge-021/paulo-custodio/test.pl
new file mode 100644
index 0000000000..377db76cbb
--- /dev/null
+++ b/challenge-021/paulo-custodio/test.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.030;
+use Test::More;
+
+is capture("perl perl/ch-1.pl"), "2.71828182845905\n";
+
+is capture('perl perl/ch-2.pl http://example.com/foo%2a'),
+ 'http://example.com/foo%2A'."\n";
+is capture('perl perl/ch-2.pl HTTP://User@Example.COM/Foo'),
+ 'http://User@example.com/Foo'."\n";
+is capture('perl perl/ch-2.pl HTTP://Example.COM/Foo'),
+ 'http://example.com/Foo'."\n";
+is capture('perl perl/ch-2.pl http://example.com/%7Efoo%2ebar'),
+ 'http://example.com/~foo.bar'."\n";
+is capture('perl perl/ch-2.pl http://example.com/foo/./bar/baz/../qux'),
+ 'http://example.com/foo/bar/qux'."\n";
+is capture('perl perl/ch-2.pl http://example.com'),
+ 'http://example.com/'."\n";
+is capture('perl perl/ch-2.pl http://example.com:80/'),
+ 'http://example.com/'."\n";
+
+done_testing;
+
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \r\t]*\n/\n/g;
+ return $out;
+}