aboutsummaryrefslogtreecommitdiff
path: root/challenge-021/guillermo-ramos
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-08-17 06:12:51 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-08-17 06:12:51 +0100
commit46ee69aabfaa3c9549b9254d3d253c28a0d96c91 (patch)
treea13bbef9e08b6a91f96a184fd7ff8fb2693f16aa /challenge-021/guillermo-ramos
parentec5563f624bec6c302622c020496644d28d0b26f (diff)
downloadperlweeklychallenge-club-46ee69aabfaa3c9549b9254d3d253c28a0d96c91.tar.gz
perlweeklychallenge-club-46ee69aabfaa3c9549b9254d3d253c28a0d96c91.tar.bz2
perlweeklychallenge-club-46ee69aabfaa3c9549b9254d3d253c28a0d96c91.zip
- Added solutions by Guillermo Ramos.
Diffstat (limited to 'challenge-021/guillermo-ramos')
-rw-r--r--challenge-021/guillermo-ramos/perl5/ch-1.pl19
-rw-r--r--challenge-021/guillermo-ramos/perl5/ch-2.pl62
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-021/guillermo-ramos/perl5/ch-1.pl b/challenge-021/guillermo-ramos/perl5/ch-1.pl
new file mode 100644
index 0000000000..86b5ef3e22
--- /dev/null
+++ b/challenge-021/guillermo-ramos/perl5/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+#
+# 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;
+
+my $ITERS = shift or die "Usage: $0 <iterations>";
+
+my $e = 1;
+my $denom = 1;
+foreach my $i (1 .. $ITERS) {
+ $denom *= $i;
+ $e += 1/$denom;
+}
+
+print "$e\n";
diff --git a/challenge-021/guillermo-ramos/perl5/ch-2.pl b/challenge-021/guillermo-ramos/perl5/ch-2.pl
new file mode 100644
index 0000000000..a3970a2c7a
--- /dev/null
+++ b/challenge-021/guillermo-ramos/perl5/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+#
+# Write a script for URL normalization based on rfc3986
+#
+# 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.
+#
+# (https://en.wikipedia.org/wiki/URL_normalization).
+################################################################################
+
+use strict;
+use warnings;
+
+my $url = shift or die "Usage: $0 <url>\n";
+
+my $scheme_re = qr<([hH][tT][tT][pP][sS]?)>;
+my $ipv4_re = qr<(?:[0-9]{1,3}\.){3}[0-9]{1,3}>;
+my $hostname_re = qr<[a-zA-Z0-9.\-]+>;
+my $host_re = qr<($ipv4_re|$hostname_re)>;
+my $port_re = qr<(?::([0-9]+))?>;
+my $rest_re = qr<([a-zA-Z0-9/\-._~\?=&%#]*)>;
+
+# Capitalizing letters in escape sequences
+$url =~ s<%[a-zA-Z0-9]{2}>< uc($&) >ge;
+
+sub decode_octet {
+ my $octet = shift;
+ my $hex = hex(substr($octet, 1, length($octet)));
+ if (grep /^$hex$/, map ord, 'a'..'z', 'A'..'Z', '0'..'9', qw<- . _ ~>) {
+ return chr($hex);
+ } else {
+ return $octet;
+ }
+}
+
+# Decoding percent-encoded octets of unreserved characters
+$url =~ s<%[a-zA-Z0-9]{2}>< decode_octet($&) >ge;
+
+my ($scheme, $host, $port, $path) = $url =~
+ m<^$scheme_re://$host_re$port_re$rest_re>;
+
+die "Unable to decode URI" unless $scheme and $host;
+
+# Converting the scheme and host to lower case
+$scheme = lc($scheme);
+$host = lc($host);
+
+# Removing the default port
+if (!$port || $scheme eq 'http' && $port == 80 || $scheme eq 'https' && $port == 443) {
+ $port = '';
+} else {
+ $port = ":$port";
+}
+
+# Removing dot-segments
+$path =~ s</\./|/\.$></>g; # Remove all '.'
+$path =~ s</[^/]+/\.\.><>g; # Remove instances of the pattern: '/x/..'
+$path =~ s<(\.\./)+><>g; # Remove all '..'s left at the beginning
+
+print "$scheme://$host$port$path\n";