aboutsummaryrefslogtreecommitdiff
path: root/challenge-021
diff options
context:
space:
mode:
authorNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-08-17 21:37:27 +0200
committerNoud Aldenhoven <noud.aldenhoven@gmail.com>2019-08-17 21:37:27 +0200
commitfc8cf1088931041f4bb61625e90d606613fa4328 (patch)
treeee7ae9be525d57be7f96d790e97a3a9894d984e0 /challenge-021
parentf997fb18f9f7949b181fa5a445235ce32303e0d0 (diff)
downloadperlweeklychallenge-club-fc8cf1088931041f4bb61625e90d606613fa4328.tar.gz
perlweeklychallenge-club-fc8cf1088931041f4bb61625e90d606613fa4328.tar.bz2
perlweeklychallenge-club-fc8cf1088931041f4bb61625e90d606613fa4328.zip
Solutions to challenge 021 problem 1 and 2 in Perl 6 by Noud
Diffstat (limited to 'challenge-021')
-rw-r--r--challenge-021/noud/perl6/ch-1.p627
-rw-r--r--challenge-021/noud/perl6/ch-2.p661
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-021/noud/perl6/ch-1.p6 b/challenge-021/noud/perl6/ch-1.p6
new file mode 100644
index 0000000000..0e1b47b32f
--- /dev/null
+++ b/challenge-021/noud/perl6/ch-1.p6
@@ -0,0 +1,27 @@
+# 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.
+
+
+# We approximate Euler's number with a continuous fraction. First we create
+# the infinite continuous array representing the continous fraction. I.e.
+#
+# @exp_cf = [2; 1, 2, 1, 1, 4, 1, 1, 6, 1, ..., 1, 2n, 1, ...].
+
+my @exp_cf = 2, |(2..Inf).map({ if ($_ % 3) != 0 { 1 } else { 2/3 * $_ } });
+
+
+# cf2r converts a (finite) truncation of the continuous array to a float.
+
+sub cf2r(@cf) {
+ if @cf.elems > 0 {
+ @cf[0] + 1 / cf2r(@cf[1..*]);
+ } else {
+ 1;
+ }
+}
+
+
+# To approximate Euler's constant we take the first 50 numbers of the
+# continuous fraction.
+
+cf2r(@exp_cf[^50]).say;
diff --git a/challenge-021/noud/perl6/ch-2.p6 b/challenge-021/noud/perl6/ch-2.p6
new file mode 100644
index 0000000000..77cdcba36d
--- /dev/null
+++ b/challenge-021/noud/perl6/ch-2.p6
@@ -0,0 +1,61 @@
+# 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 Test;
+
+sub normalize_url($url is copy) {
+ given $url {
+ s:g/(\w+)/{lc $0}/; # Convert the scheme and host to lower case.
+ s:g/\%(<xdigit>**2)/\%{uc $0}/; # Capitalizing letters in escape squences.
+ s:g/\%(<[4..7]><xdigit>)/{:16(~$0).chr}/; # Decode ALPHA
+ s:g/\%(3\d)/{:16(~$0).chr}/; # Decode DIGIT
+ s:g/\%2D/-/; # Decode hyphen.
+ s:g/\%2E/./; # Decode period.
+ s:g/\%5F/_/; # Decode underscore.
+ s:g/\%7E/~/; # Decode tilde.
+ s/\:80//; # Removing the default port.
+ s:g/\/\.\//\//; # Removing dot-segment '.'.
+ s:g/\/\.\.\//\//; # Removing dot-segment '..'.
+ s/^https/http/; # Limiting protocols.
+ s:g/<!after http\:>\/\//\//; # Remove duplicated slashes.
+ };
+ return $url;
+}
+
+
+sub MAIN() {
+ plan 7;
+
+ my $exp1 = "HTTP://www.Example.com/";
+ my $ret1 = "http://www.example.com/";
+ ok normalize_url($exp1) === $ret1;
+
+ my $exp2 = "http://www.example.com/a%c2%b1b";
+ my $ret2 = "http://www.example.com/a%C2%B1b";
+ ok normalize_url($exp2) === $ret2;
+
+ my $exp3 = "http://www.example.com/%2D%2E%5F%7E%41%2D";
+ my $ret3 = "http://www.example.com/-._~A-";
+ ok normalize_url($exp3) === $ret3;
+
+ my $exp4 = "http://www.example.com:80/";
+ my $ret4 = "http://www.example.com/";
+ ok normalize_url($exp4) === $ret4;
+
+ my $exp5 = "http://www.example.com/../a/b/../c/./d.html";
+ my $ret5 = "http://www.example.com/a/b/c/d.html";
+ ok normalize_url($exp5) === $ret5;
+
+ my $exp6 = "https://www.example.com/https";
+ my $ret6 = "http://www.example.com/https";
+ ok normalize_url($exp6) === $ret6;
+
+ my $exp7 = "http://www.example.com//a";
+ my $ret7 = "http://www.example.com/a";
+ ok normalize_url($exp7) === $ret7;
+}