aboutsummaryrefslogtreecommitdiff
path: root/challenge-021
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-24 19:46:21 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-24 19:46:21 +0000
commitc4b1cc1f94522a4ea4958ef9f10af9b6bebce78a (patch)
tree8956b9ed7c0a3c7826cb42509de2acc2070d25bb /challenge-021
parent05ec7ce53c5bb5bb898c992d782ddc322099cdf7 (diff)
downloadperlweeklychallenge-club-c4b1cc1f94522a4ea4958ef9f10af9b6bebce78a.tar.gz
perlweeklychallenge-club-c4b1cc1f94522a4ea4958ef9f10af9b6bebce78a.tar.bz2
perlweeklychallenge-club-c4b1cc1f94522a4ea4958ef9f10af9b6bebce78a.zip
Add Python solution to challenge 21
Diffstat (limited to 'challenge-021')
-rw-r--r--challenge-021/paulo-custodio/Makefile2
-rw-r--r--challenge-021/paulo-custodio/perl/ch-1.pl6
-rw-r--r--challenge-021/paulo-custodio/python/ch-1.py22
-rw-r--r--challenge-021/paulo-custodio/python/ch-2.py61
-rw-r--r--challenge-021/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-021/paulo-custodio/t/test-2.yaml35
-rw-r--r--challenge-021/paulo-custodio/test.pl31
7 files changed, 128 insertions, 34 deletions
diff --git a/challenge-021/paulo-custodio/Makefile b/challenge-021/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-021/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-021/paulo-custodio/perl/ch-1.pl b/challenge-021/paulo-custodio/perl/ch-1.pl
index ee4a425f69..b189473da6 100644
--- a/challenge-021/paulo-custodio/perl/ch-1.pl
+++ b/challenge-021/paulo-custodio/perl/ch-1.pl
@@ -3,8 +3,8 @@
# 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.
+# 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 Modern::Perl;
@@ -18,4 +18,4 @@ sub calc_e {
return $e;
}
-say calc_e();
+say sprintf("%.14f", calc_e());
diff --git a/challenge-021/paulo-custodio/python/ch-1.py b/challenge-021/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..134b371ad3
--- /dev/null
+++ b/challenge-021/paulo-custodio/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python3
+
+# 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.
+
+def calc_e():
+ e = 1
+ n = 0
+ prod = 1
+ prev = 0
+ while prev != e:
+ prev = e
+ n += 1
+ prod *= n
+ e += 1/prod
+
+ return e
+
+print("{:.14f}".format(calc_e()))
diff --git a/challenge-021/paulo-custodio/python/ch-2.py b/challenge-021/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..0c6b7a4325
--- /dev/null
+++ b/challenge-021/paulo-custodio/python/ch-2.py
@@ -0,0 +1,61 @@
+#!/usr/bin/python3
+
+# 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.
+
+import sys
+import re
+
+def decode_triplets(hex):
+ c = chr(int(hex, 16))
+ if re.match(r"[a-zA-Z0-9\-._~]", c):
+ return c
+ else:
+ return '%'+hex.upper()
+
+def upper_repl(matchobj):
+ return matchobj.group(0).upper()
+
+def sheme_host_repl(matchobj):
+ return matchobj.group(1).lower()+matchobj.group(2)+matchobj.group(4).lower()
+
+def decode_triplets_repl(matchobj):
+ return decode_triplets(matchobj.group(1))
+
+def norm_uri(uri):
+ # Converting percent-encoded triplets to uppercase
+ uri = re.sub(r"%[0-9a-f]{2}", upper_repl, uri, flags=re.IGNORECASE)
+
+ # Converting the scheme and host to lowercase
+ uri = re.sub(r"^(\w+://)((.*?@)?)(.*?/)", sheme_host_repl, uri)
+
+ # Decoding percent-encoded triplets of unreserved characters
+ uri = re.sub(r"%([0-9a-f]{2})", decode_triplets_repl, uri, flags=re.IGNORECASE)
+
+ # Removing dot-segments
+ while True:
+ uri, count = re.subn(r"/\./", "/", uri, count=1)
+ if count==0:
+ break
+ while True:
+ uri, count = re.subn(r"/[^/]+/\.\./", "/", uri, count=1)
+ if count==0:
+ break
+
+ # Converting an empty path to a "/" path
+ uri = re.sub(r"^(\w+://[^/]+)$", r"\1/", uri)
+
+ # Removing the default port
+ uri = re.sub(r"^(http://[^/]+?):80/", r"\1/", uri)
+
+ return uri
+
+print(norm_uri(sys.argv[1]))
diff --git a/challenge-021/paulo-custodio/t/test-1.yaml b/challenge-021/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..0e2aae9794
--- /dev/null
+++ b/challenge-021/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 2.71828182845905
diff --git a/challenge-021/paulo-custodio/t/test-2.yaml b/challenge-021/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..17b4ba4c62
--- /dev/null
+++ b/challenge-021/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,35 @@
+- setup:
+ cleanup:
+ args: http://example.com/foo%2a
+ input:
+ output: http://example.com/foo%2A
+- setup:
+ cleanup:
+ args: HTTP://User@Example.COM/Foo
+ input:
+ output: http://User@example.com/Foo
+- setup:
+ cleanup:
+ args: HTTP://Example.COM/Foo
+ input:
+ output: http://example.com/Foo
+- setup:
+ cleanup:
+ args: http://example.com/%7Efoo%2ebar
+ input:
+ output: http://example.com/~foo.bar
+- setup:
+ cleanup:
+ args: http://example.com/foo/./bar/baz/../qux
+ input:
+ output: http://example.com/foo/bar/qux
+- setup:
+ cleanup:
+ args: http://example.com
+ input:
+ output: http://example.com/
+- setup:
+ cleanup:
+ args: http://example.com:80/
+ input:
+ output: http://example.com/
diff --git a/challenge-021/paulo-custodio/test.pl b/challenge-021/paulo-custodio/test.pl
deleted file mode 100644
index ac2d794fd7..0000000000
--- a/challenge-021/paulo-custodio/test.pl
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-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;
-}