aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-25 16:50:29 +0000
committerGitHub <noreply@github.com>2021-11-25 16:50:29 +0000
commit997646c13451c37947d988d1ef2441b44dbeff35 (patch)
tree17ae9d87d29fff830b30c0caa15f69a258b8c9f0
parent77a5374cbf867e68721ec5de53a063f9c7a1b745 (diff)
parentc4b1cc1f94522a4ea4958ef9f10af9b6bebce78a (diff)
downloadperlweeklychallenge-club-997646c13451c37947d988d1ef2441b44dbeff35.tar.gz
perlweeklychallenge-club-997646c13451c37947d988d1ef2441b44dbeff35.tar.bz2
perlweeklychallenge-club-997646c13451c37947d988d1ef2441b44dbeff35.zip
Merge pull request #5280 from pauloscustodio/devel
Devel
-rw-r--r--challenge-020/paulo-custodio/Makefile2
-rw-r--r--challenge-020/paulo-custodio/perl/ch-1.pl6
-rw-r--r--challenge-020/paulo-custodio/python/ch-1.py22
-rw-r--r--challenge-020/paulo-custodio/python/ch-2.py34
-rw-r--r--challenge-020/paulo-custodio/t/test-1.yaml6
-rw-r--r--challenge-020/paulo-custodio/t/test-2.yaml5
-rw-r--r--challenge-020/paulo-custodio/test.pl20
-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
14 files changed, 200 insertions, 57 deletions
diff --git a/challenge-020/paulo-custodio/Makefile b/challenge-020/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-020/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-020/paulo-custodio/perl/ch-1.pl b/challenge-020/paulo-custodio/perl/ch-1.pl
index 4940788eb0..64fcbe2d9d 100644
--- a/challenge-020/paulo-custodio/perl/ch-1.pl
+++ b/challenge-020/paulo-custodio/perl/ch-1.pl
@@ -4,8 +4,8 @@
#
# Task #1
# Write a script to accept a string from command line and split it on change
-# of character. For example, if the string is “ABBCDEEF”, then it should split
-# like “A”, “BB”, “C”, “D”, “EE”, “F”.
+# of character. For example, if the string is "ABBCDEEF", then it should split
+# like "A", "BB", "C", "D", "EE", "F".
use Modern::Perl;
@@ -17,4 +17,4 @@ while ($str ne '') {
push @segs, $1;
}
-say join(", ", map {'"'.$_.'"'} @segs), ".";
+say join(", ", map {'"'.$_.'"'} @segs);
diff --git a/challenge-020/paulo-custodio/python/ch-1.py b/challenge-020/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..350bd40aa8
--- /dev/null
+++ b/challenge-020/paulo-custodio/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/python3
+
+# Challenge 020
+#
+# Task #1
+# Write a script to accept a string from command line and split it on change
+# of character. For example, if the string is "ABBCDEEF", then it should split
+# like "A", "BB", "C", "D", "EE", "F".
+
+import sys
+import re
+
+str = sys.argv[1]
+segs = []
+while True:
+ matches = re.match(r"((.)\2*)", str)
+ if not matches:
+ break
+ segs.append(matches.group(1))
+ str = str[matches.end(0):]
+
+print(", ".join(['"'+x+'"' for x in segs]))
diff --git a/challenge-020/paulo-custodio/python/ch-2.py b/challenge-020/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..1f67e330b9
--- /dev/null
+++ b/challenge-020/paulo-custodio/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/python3
+
+# Challenge 020
+#
+# Task #2
+# Write a script to print the smallest pair of Amicable Numbers. For more
+# information, please checkout wikipedia page.
+
+import math
+
+def divisors(n):
+ div_low = []
+ div_high = []
+ for i in range(1, int(math.sqrt(n)+1)):
+ if n%i==0:
+ div_low.append(i)
+ if n/i!=i:
+ div_high.insert(0, int(n/i))
+ return [*div_low, *div_high]
+
+def proper_divisors(n):
+ return filter(lambda x:x!=n, divisors(n))
+
+def smallest_amicable_pair():
+ n = 1
+ while True:
+ n += 1
+ sum1 = sum(proper_divisors(n))
+ sum2 = sum(proper_divisors(sum1))
+ if sum2==n and n<sum1:
+ return n, sum1
+
+a, b = smallest_amicable_pair()
+print(f"({a},{b})")
diff --git a/challenge-020/paulo-custodio/t/test-1.yaml b/challenge-020/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..34c539ace7
--- /dev/null
+++ b/challenge-020/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,6 @@
+- setup:
+ cleanup:
+ args: ABBCDEEF
+ input:
+ output: |
+ "A", "BB", "C", "D", "EE", "F"
diff --git a/challenge-020/paulo-custodio/t/test-2.yaml b/challenge-020/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..5eb7bc5230
--- /dev/null
+++ b/challenge-020/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: (220,284)
diff --git a/challenge-020/paulo-custodio/test.pl b/challenge-020/paulo-custodio/test.pl
deleted file mode 100644
index 2a03c35332..0000000000
--- a/challenge-020/paulo-custodio/test.pl
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-is capture("perl perl/ch-1.pl ABBCDEEF"), <<END;
-"A", "BB", "C", "D", "EE", "F".
-END
-
-is capture("perl perl/ch-2.pl"), "(220,284)\n";
-
-done_testing;
-
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \r\t]*\n/\n/g;
- return $out;
-}
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;
-}