diff options
| author | Niels van Dijke <65567640+PerlBoy1967@users.noreply.github.com> | 2021-11-28 23:27:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-28 23:27:52 +0100 |
| commit | 9cc19213f6d3bd000f9580f8981ae6b79f202379 (patch) | |
| tree | b86967b049e6357bc69ee5e1999a29ad4ebbba99 /challenge-021/paulo-custodio/python | |
| parent | 87aa8152be4cf2d9b0c65caa07a9070bd5ceceea (diff) | |
| parent | fb0fe52f0a11011dc5baaee81034a888a044b811 (diff) | |
| download | perlweeklychallenge-club-9cc19213f6d3bd000f9580f8981ae6b79f202379.tar.gz perlweeklychallenge-club-9cc19213f6d3bd000f9580f8981ae6b79f202379.tar.bz2 perlweeklychallenge-club-9cc19213f6d3bd000f9580f8981ae6b79f202379.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-021/paulo-custodio/python')
| -rw-r--r-- | challenge-021/paulo-custodio/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-021/paulo-custodio/python/ch-2.py | 61 |
2 files changed, 83 insertions, 0 deletions
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])) |
