aboutsummaryrefslogtreecommitdiff
path: root/challenge-021
diff options
context:
space:
mode:
authorDaniel Mantovani <daniel@gmail.com>2019-08-17 16:27:42 -0300
committerDaniel Mantovani <daniel@gmail.com>2019-08-17 16:27:42 -0300
commite068240f86fc0d489f85d215894947f3d9e6ff76 (patch)
treec887100bd1a4ddfcd8000c7a669fb7c330cec7a4 /challenge-021
parent08490371aff671680d216ff0a5b792cf997c9d47 (diff)
downloadperlweeklychallenge-club-e068240f86fc0d489f85d215894947f3d9e6ff76.tar.gz
perlweeklychallenge-club-e068240f86fc0d489f85d215894947f3d9e6ff76.tar.bz2
perlweeklychallenge-club-e068240f86fc0d489f85d215894947f3d9e6ff76.zip
my proposed solutions for challenge-021, p5 1 & 2
Diffstat (limited to 'challenge-021')
-rw-r--r--challenge-021/daniel-mantovani/perl5/ch-1.pl33
-rw-r--r--challenge-021/daniel-mantovani/perl5/ch-2.pl54
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-021/daniel-mantovani/perl5/ch-1.pl b/challenge-021/daniel-mantovani/perl5/ch-1.pl
new file mode 100644
index 0000000000..b2fcef5b53
--- /dev/null
+++ b/challenge-021/daniel-mantovani/perl5/ch-1.pl
@@ -0,0 +1,33 @@
+# 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 v5.10;
+
+# As Perl already has a "exp" function (see https://perldoc.perl.org/5.30.0/functions/exp.html)
+# we could just use it like:
+#
+# say exp(1);
+#
+# but I'm pretty sure this was not the intent of the challenge :)
+#
+# So we will are going to calculate the series formula:
+#
+# e = 1 + 1/1 + 1/(1 *2) + 1/(1 * 2 * 3) + 1/(1 * 2 * 3 * 4) + ... +
+#
+# until our sum don't grow anymore, and that will be our approximation to the result
+#
+# Note that each term could be calculated dividing the former term by a growing integer
+#
+my $e = 1; # this will be our final value. We already have the first "1" on it
+my $n = 1; # this will be the term number, incrementing after we add a new term
+my $t = 1; # this is current term. It will be divided by $n to get following one
+
+# we will stop calculation when $t is so small it is considered as a 0 by Perl internal
+# math
+while ($t) {
+ $t /= $n++; # calculate new term. Also we increment $n for next step
+ $e += $t; # and just keep adding new terms
+}
+say $e;
diff --git a/challenge-021/daniel-mantovani/perl5/ch-2.pl b/challenge-021/daniel-mantovani/perl5/ch-2.pl
new file mode 100644
index 0000000000..3d0e09cd87
--- /dev/null
+++ b/challenge-021/daniel-mantovani/perl5/ch-2.pl
@@ -0,0 +1,54 @@
+# 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 v5.10;
+
+# After reading over RFC 3968, I think is much better to use a good module from CPAN
+# for this kind of job. There are just too many cases to consider, so reinventing
+# the wheel doesn't seem to be a good option here.
+#
+# There are two modules that catch my attention for RFC 3986 normalization:
+# URI::Normalize
+# URL::Normalize
+#
+# last one is heavier on dependences than the former, but I see it allows
+# much more flexibility to fine tune the desired normalization
+
+use URL::Normalize;
+
+my $in = shift; # get url from command line
+die "Usage: perl $0 <url-to-be-normalized>" unless $in;
+
+# first we initialize our normalizer with input url
+my $normalizer = URL::Normalize->new($in);
+
+# now we start to apply some normalizations.
+# some methods just have an obvious meaning
+$normalizer->remove_dot_segments;
+$normalizer->remove_duplicate_slashes;
+$normalizer->sort_query_parameters;
+$normalizer->remove_duplicate_query_parameters;
+$normalizer->remove_empty_query_parameters;
+
+# The "make_canonical" method, involves several important steps like
+# convert scheme & host to lower case, capitalize escape seqs,
+# decode all unreserved chars, and remove default port when possible
+
+$normalizer->make_canonical;
+
+# and that's it, now we just print the normalized result:
+say $normalizer->url;
+
+# There are a lot of very interestin methods on this module, that will
+# allow to normalize removing several parameters that will not really
+# change the content we will get using the url.
+
+# Please take a look at https://metacpan.org/pod/URL::Normalize for
+# further information on the module used