From 05ec7ce53c5bb5bb898c992d782ddc322099cdf7 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Wed, 24 Nov 2021 11:38:35 +0000 Subject: Add Python solution to challenge 20 --- challenge-020/paulo-custodio/Makefile | 2 ++ challenge-020/paulo-custodio/perl/ch-1.pl | 6 ++--- challenge-020/paulo-custodio/python/ch-1.py | 22 +++++++++++++++++++ challenge-020/paulo-custodio/python/ch-2.py | 34 +++++++++++++++++++++++++++++ challenge-020/paulo-custodio/t/test-1.yaml | 6 +++++ challenge-020/paulo-custodio/t/test-2.yaml | 5 +++++ challenge-020/paulo-custodio/test.pl | 20 ----------------- 7 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 challenge-020/paulo-custodio/Makefile create mode 100644 challenge-020/paulo-custodio/python/ch-1.py create mode 100644 challenge-020/paulo-custodio/python/ch-2.py create mode 100644 challenge-020/paulo-custodio/t/test-1.yaml create mode 100644 challenge-020/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-020/paulo-custodio/test.pl 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 Date: Wed, 24 Nov 2021 19:46:21 +0000 Subject: Add Python solution to challenge 21 --- challenge-021/paulo-custodio/Makefile | 2 + challenge-021/paulo-custodio/perl/ch-1.pl | 6 +-- challenge-021/paulo-custodio/python/ch-1.py | 22 +++++++++++ challenge-021/paulo-custodio/python/ch-2.py | 61 +++++++++++++++++++++++++++++ challenge-021/paulo-custodio/t/test-1.yaml | 5 +++ challenge-021/paulo-custodio/t/test-2.yaml | 35 +++++++++++++++++ challenge-021/paulo-custodio/test.pl | 31 --------------- 7 files changed, 128 insertions(+), 34 deletions(-) create mode 100644 challenge-021/paulo-custodio/Makefile create mode 100644 challenge-021/paulo-custodio/python/ch-1.py create mode 100644 challenge-021/paulo-custodio/python/ch-2.py create mode 100644 challenge-021/paulo-custodio/t/test-1.yaml create mode 100644 challenge-021/paulo-custodio/t/test-2.yaml delete mode 100644 challenge-021/paulo-custodio/test.pl 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; -} -- cgit