aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-08-17 06:07:08 +0100
committerGitHub <noreply@github.com>2019-08-17 06:07:08 +0100
commit67c36567b9824e66cc48c1e945cc84f09f2f7cb4 (patch)
tree0e5e5028e58daac84d6938d7e1e32539072178a6
parent08490371aff671680d216ff0a5b792cf997c9d47 (diff)
parentbcc6cfc303c6343606f2ce75762c403a385feb52 (diff)
downloadperlweeklychallenge-club-67c36567b9824e66cc48c1e945cc84f09f2f7cb4.tar.gz
perlweeklychallenge-club-67c36567b9824e66cc48c1e945cc84f09f2f7cb4.tar.bz2
perlweeklychallenge-club-67c36567b9824e66cc48c1e945cc84f09f2f7cb4.zip
Merge pull request #515 from andrezgz/challenge-021
Challenge 021
-rw-r--r--challenge-021/andrezgz/perl5/ch-1.pl19
-rw-r--r--challenge-021/andrezgz/perl5/ch-2.pl92
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-021/andrezgz/perl5/ch-1.pl b/challenge-021/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..c7bb8de8d7
--- /dev/null
+++ b/challenge-021/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-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.
+# https://en.wikipedia.org/wiki/E_(mathematical_constant)
+
+use strict;
+use warnings;
+
+my $e_aprox = 0;
+my $fact = 1;
+for (1, 1 .. 100) { #tricky - additional 1 to represent 0!
+ $fact *= $_;
+ $e_aprox += 1/$fact;
+}
+
+print $e_aprox;
diff --git a/challenge-021/andrezgz/perl5/ch-2.pl b/challenge-021/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..b20bb2c2da
--- /dev/null
+++ b/challenge-021/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-021/
+# Task #2
+# Write a script for URL normalization based on rfc3986. This task was shared by Anonymous Contributor.
+# https://en.wikipedia.org/wiki/URL_normalization
+# 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;
+
+my $url_in = $ARGV[0]
+ || 'HTTPS://www.Example.com:443/a%c2%b1b/%7Eandrezgz/bar.html?pi=3%2e14#hi%2Dthere';
+
+print 'Original:'.$/;
+print $url_in.$/.$/;
+
+my ($scheme, $auth, $path, $query, $frag, $userinfo, $host, $port);
+
+($scheme, $auth, $path, $query, $frag) = uri_split($url_in);
+($userinfo, $host, $port) = auth_split($auth) if $auth;
+
+# Normalizations that preserve semantics
+
+## Converts the scheme and host to lower case.
+($scheme, $host) = map { lc $_ } ($scheme, $host);
+
+## Capitalizes letters in escape sequences.
+## Decodes percent-encoded octets of unreserved characters.
+($path, $query, $frag) =
+ map { my $comp = $_;
+ $comp =~ s{%([A-Fa-f0-9]{2})}
+ { my $chr = chr(hex($1));
+ $chr =~ /^[A-Za-z0-9\-_\.~]$/ ? $chr : '%'.uc $1;
+ }ge if $comp;
+ $comp;
+ } ($path, $query, $frag);
+
+## Removes the default port (port 80 for http).
+my %default_ports = (
+ ftp => 21, gopher => 70, http => 80, https => 443,
+ ldap => 389, ldaps => 636, mms => 1755, news => 119,
+ pop => 110, rlogin => 513, rsync => 873, rtsp => 554,
+ rstpu => 554, sip => 5060, sips => 5061, snews => 563,
+ ssh => 22, telnet => 23, tn3270 => 23,
+);
+$port = '' if ($port == $default_ports{$scheme});
+
+print 'Normalized:'.$/;
+print uri_join($scheme, $userinfo, $host, $port, $path, $query, $frag).$/;
+
+
+sub uri_join {
+ my ($scheme, $userinfo, $host, $port, $path, $query, $frag) = @_;
+
+ my $uri = $scheme . ':';
+ if ($host) {
+ $uri .= '//';
+ $uri .= $userinfo . '@' if ($userinfo);
+ $uri .= $host;
+ $uri .= ':' . $port if ($port);
+ }
+ $uri .= $path;
+ $uri .= '?' . $query if ($query);
+ $uri .= '#' . $frag if ($frag);
+
+ return $uri;
+}
+
+
+# functions created in challenge-017/andrezgz/perl5/ch-2.pl
+sub uri_split {
+ return $_[0] =~ m|
+ ^ # string start
+ ([^/?\#]+) : # scheme
+ (?: // ([^/?\#]*) )? # authority (optional)
+ ([^?\#]*) # path
+ (?: \? ([^\#]*) )? # query (optional)
+ (?: \# (.*) )? # fragment (optional)
+ $ # string end
+ |x;
+}
+
+sub auth_split {
+ return $_[0] =~ m|
+ (?: ([^@]+) @ )? # userinfo (optional)
+ ([^:/?\#]+) # host
+ (?: : (\d+) )? # port (optional)
+ |x;
+}