diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-19 18:53:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-19 18:53:34 +0000 |
| commit | ea83790fdf3c76091e38faaf27cade5240780104 (patch) | |
| tree | ce42a0759388f5979c55a6ca208a198ff0d5a8d2 | |
| parent | 76736d8d34efe6885603d15f130a6171b109c346 (diff) | |
| parent | c41cbd7803555cc523ed97adcff8e7652f4a6444 (diff) | |
| download | perlweeklychallenge-club-ea83790fdf3c76091e38faaf27cade5240780104.tar.gz perlweeklychallenge-club-ea83790fdf3c76091e38faaf27cade5240780104.tar.bz2 perlweeklychallenge-club-ea83790fdf3c76091e38faaf27cade5240780104.zip | |
Merge pull request #5248 from pauloscustodio/devel
Devel
| -rw-r--r-- | challenge-016/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-016/paulo-custodio/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-016/paulo-custodio/python/ch-2.py | 25 | ||||
| -rw-r--r-- | challenge-016/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-016/paulo-custodio/t/test-2.yaml | 30 | ||||
| -rw-r--r-- | challenge-016/paulo-custodio/test.pl | 27 | ||||
| -rw-r--r-- | challenge-017/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-017/paulo-custodio/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-017/paulo-custodio/python/ch-2.py | 87 | ||||
| -rw-r--r-- | challenge-017/paulo-custodio/t/test-1.yaml | 100 | ||||
| -rw-r--r-- | challenge-017/paulo-custodio/t/test-2.yaml | 72 | ||||
| -rw-r--r-- | challenge-017/paulo-custodio/test.pl | 98 |
12 files changed, 387 insertions, 125 deletions
diff --git a/challenge-016/paulo-custodio/Makefile b/challenge-016/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-016/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-016/paulo-custodio/python/ch-1.py b/challenge-016/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..e4ce675a5d --- /dev/null +++ b/challenge-016/paulo-custodio/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +# Challenge 016 +# +# Task #1 +# Pythagoras Pie Puzzle, proposed by Jo Christian Oterhals. +# +# At a party a pie is to be shared by 100 guest. The first guest gets 1% of the +# pie, the second guest gets 2% of the remaining pie, the third gets 3% of the +# remaining pie, the fourth gets 4% and so on. +# +# Write a script that figures out which guest gets the largest piece of pie. + +import operator + +class Slice(): + def __init__(self, guest, slice): + self.guest = guest + self.slice = slice + def __str__(self): + return f"Slice({self.guest}, {self.slice})" + +slices = [] +pie = 1 +for guest in range(1, 101): + slice = guest/100*pie + pie -= slice + slices.append(Slice(guest, slice)) +slices.sort(key=operator.attrgetter('slice')) +slice = slices[-1] + +print("Guest {} gets {:.4f}% of the pie.".format(slice.guest, 100*slice.slice)) diff --git a/challenge-016/paulo-custodio/python/ch-2.py b/challenge-016/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..dea3be6947 --- /dev/null +++ b/challenge-016/paulo-custodio/python/ch-2.py @@ -0,0 +1,25 @@ +#!/usr/bin/python3 + +# Challenge 016 +# +# Task #2 +# Write a script to validate a given bitcoin address. Most Bitcoin addresses +# are 34 characters. They consist of random digits and uppercase and lowercase +# letters, with the exception that the uppercase letter "O", uppercase letter "I", +# lowercase letter "l", and the number "0" are never used to prevent visual +# ambiguity. A bitcoin address encodes 25 bytes. The last four bytes are a +# checksum check. They are the first four bytes of a double SHA-256 digest +# of the previous 21 bytes. For more information, please refer wiki page. +# Here are some valid bitcoin addresses: +# +# 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 +# 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy + +import sys +from cryptoaddress import BitcoinAddress + +try: + bitcoin_address = BitcoinAddress(sys.argv[1], network_type='mainnet') + print(1) +except ValueError: + print(0) diff --git a/challenge-016/paulo-custodio/t/test-1.yaml b/challenge-016/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..a2751e2584 --- /dev/null +++ b/challenge-016/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: Guest 10 gets 6.2816% of the pie. diff --git a/challenge-016/paulo-custodio/t/test-2.yaml b/challenge-016/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f45312b812 --- /dev/null +++ b/challenge-016/paulo-custodio/t/test-2.yaml @@ -0,0 +1,30 @@ +- setup: + cleanup: + args: 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 + input: + output: 1 +- setup: + cleanup: + args: 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy + input: + output: 1 +- setup: + cleanup: + args: 2BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 + input: + output: 0 +- setup: + cleanup: + args: 4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy + input: + output: 0 +- setup: + cleanup: + args: 1lvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2 + input: + output: 0 +- setup: + cleanup: + args: 3O98t1WpEZ73CNmQviecrnyiWrnqRhWNLy + input: + output: 0 diff --git a/challenge-016/paulo-custodio/test.pl b/challenge-016/paulo-custodio/test.pl deleted file mode 100644 index 1017049192..0000000000 --- a/challenge-016/paulo-custodio/test.pl +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl binary"), <<END; -Guest 10 gets 6.2816% of the pie. -END - -is capture("perl perl/ch-2.pl 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"), "1\n"; -is capture("perl perl/ch-2.pl 3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy"), "1\n"; - -is capture("perl perl/ch-2.pl 2BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"), "0\n"; -is capture("perl perl/ch-2.pl 4J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy"), "0\n"; - -is capture("perl perl/ch-2.pl 1lvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"), "0\n"; -is capture("perl perl/ch-2.pl 3O98t1WpEZ73CNmQviecrnyiWrnqRhWNLy"), "0\n"; - - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} diff --git a/challenge-017/paulo-custodio/Makefile b/challenge-017/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-017/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-017/paulo-custodio/python/ch-1.py b/challenge-017/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..a8adceb3a1 --- /dev/null +++ b/challenge-017/paulo-custodio/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 + +# Challenge 017 +# +# Task #1 +# Create a script to demonstrate Ackermann function. The Ackermann function is +# defined as below, m and n are positive number: +# +# A(m, n) = n + 1 if m = 0 +# A(m, n) = A(m - 1, 1) if m > 0 and n = 0 +# A(m, n) = A(m - 1, A(m, n - 1)) if m > 0 and n > 0 +# +# Example expansions as shown in wiki page. +# +# A(1, 2) = A(0, A(1, 1)) +# = A(0, A(0, A(1, 0))) +# = A(0, A(0, A(0, 1))) +# = A(0, A(0, 2)) +# = A(0, 3) +# = 4 + +import sys + +def A(m, n): + if m==0: + return n+1 + elif m>0 and n==0: + return A(m-1, 1) + elif m>0 and n>0: + return A(m-1, A(m, n-1)) + +print(A(*[int(x) for x in sys.argv[1:3]])) diff --git a/challenge-017/paulo-custodio/python/ch-2.py b/challenge-017/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..7493986e88 --- /dev/null +++ b/challenge-017/paulo-custodio/python/ch-2.py @@ -0,0 +1,87 @@ +#!/usr/bin/python3 + +# Challenge 017 +# +# Task #2 +# Create a script to parse URL and print the components of URL. According to +# Wiki page, the URL syntax is as below: +# +# scheme:[//[userinfo@]host[:port]]path[?query][#fragment] +# +# For example: jdbc://user:password@localhost:3306/pwc?profile=true#h1 +# +# scheme: jdbc +# userinfo: user:password +# host: localhost +# port: 3306 +# path: /pwc +# query: profile=true +# fragment: h1 + +import sys +import re + +url = sys.argv[1] + +word = r"(?i:[a-z_][a-z_0-9+.-]*)" +pathre = r"(?:"+word+r"|/)+" + +# scheme(1) +matches = re.match(r"^("+word+r"):", url) +if matches: + scheme = matches.group(1) + url = url[matches.end(0):] +else: + scheme = "" + +# userinfo(1), host(2), port(3) +matches = re.match(r"^//"+ \ + r"(?:("+word+r"(?:[:].*?)?)[@])?"+ \ + r"("+word+r")"+ \ + r"(?:[:](\d+))?", url) +if matches: + if matches.group(1): + userinfo = matches.group(1) + else: + userinfo = "" + + host = matches.group(2) + + if matches.group(3): + port = matches.group(3) + else: + port = "" + + url = url[matches.end(0):] +else: + userinfo, host, port = "", "", "" + +# path(1), query(2), fragment(3) +matches = re.match(r"("+pathre+r")"+ \ + r"(?:[?]([^#]*))?"+ \ + r"(?:[#](.*))?"+ \ + r"$", url) +if matches: + path = matches.group(1) + + if matches.group(2): + query = matches.group(2) + else: + query = "" + + if matches.group(3): + fragment = matches.group(3) + else: + fragment = "" + + url = url[matches.end(0):] +else: + path, query, fragment = "", "", "" + +print("scheme: "+scheme) +print("userinfo: "+userinfo) +print("host: "+host) +print("port: "+port) +print("path: "+path) +print("query: "+query) +print("fragment: "+fragment) diff --git a/challenge-017/paulo-custodio/t/test-1.yaml b/challenge-017/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..a6525cc883 --- /dev/null +++ b/challenge-017/paulo-custodio/t/test-1.yaml @@ -0,0 +1,100 @@ +- setup: + cleanup: + args: 0 0 + input: + output: 1 +- setup: + cleanup: + args: 0 1 + input: + output: 2 +- setup: + cleanup: + args: 0 2 + input: + output: 3 +- setup: + cleanup: + args: 0 3 + input: + output: 4 +- setup: + cleanup: + args: 0 4 + input: + output: 5 +- setup: + cleanup: + args: 1 0 + input: + output: 2 +- setup: + cleanup: + args: 1 1 + input: + output: 3 +- setup: + cleanup: + args: 1 2 + input: + output: 4 +- setup: + cleanup: + args: 1 3 + input: + output: 5 +- setup: + cleanup: + args: 1 4 + input: + output: 6 +- setup: + cleanup: + args: 2 0 + input: + output: 3 +- setup: + cleanup: + args: 2 1 + input: + output: 5 +- setup: + cleanup: + args: 2 2 + input: + output: 7 +- setup: + cleanup: + args: 2 3 + input: + output: 9 +- setup: + cleanup: + args: 2 4 + input: + output: 11 +- setup: + cleanup: + args: 3 0 + input: + output: 5 +- setup: + cleanup: + args: 3 1 + input: + output: 13 +- setup: + cleanup: + args: 3 2 + input: + output: 29 +- setup: + cleanup: + args: 3 3 + input: + output: 61 +- setup: + cleanup: + args: 3 4 + input: + output: 125 diff --git a/challenge-017/paulo-custodio/t/test-2.yaml b/challenge-017/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..a1b82bbc9b --- /dev/null +++ b/challenge-017/paulo-custodio/t/test-2.yaml @@ -0,0 +1,72 @@ +- setup: + cleanup: + args: jdbc://user:password@localhost:3306/pwc?profile=true#h1 + input: + output: | + scheme: jdbc + userinfo: user:password + host: localhost + port: 3306 + path: /pwc + query: profile=true + fragment: h1 +- setup: + cleanup: + args: jdbc://localhost:3306/pwc?profile=true#h1 + input: + output: | + scheme: jdbc + userinfo: + host: localhost + port: 3306 + path: /pwc + query: profile=true + fragment: h1 +- setup: + cleanup: + args: jdbc://localhost/pwc?profile=true#h1 + input: + output: | + scheme: jdbc + userinfo: + host: localhost + port: + path: /pwc + query: profile=true + fragment: h1 +- setup: + cleanup: + args: jdbc:/pwc?profile=true#h1 + input: + output: | + scheme: jdbc + userinfo: + host: + port: + path: /pwc + query: profile=true + fragment: h1 +- setup: + cleanup: + args: jdbc:/pwc?profile=true + input: + output: | + scheme: jdbc + userinfo: + host: + port: + path: /pwc + query: profile=true + fragment: +- setup: + cleanup: + args: jdbc:/pwc + input: + output: | + scheme: jdbc + userinfo: + host: + port: + path: /pwc + query: + fragment: diff --git a/challenge-017/paulo-custodio/test.pl b/challenge-017/paulo-custodio/test.pl deleted file mode 100644 index 04329cfebe..0000000000 --- a/challenge-017/paulo-custodio/test.pl +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/perl - -use Modern::Perl; -use Test::More; - -is capture("perl perl/ch-1.pl 0 0"), "1\n"; -is capture("perl perl/ch-1.pl 0 1"), "2\n"; -is capture("perl perl/ch-1.pl 0 2"), "3\n"; -is capture("perl perl/ch-1.pl 0 3"), "4\n"; -is capture("perl perl/ch-1.pl 0 4"), "5\n"; - -is capture("perl perl/ch-1.pl 1 0"), "2\n"; -is capture("perl perl/ch-1.pl 1 1"), "3\n"; -is capture("perl perl/ch-1.pl 1 2"), "4\n"; -is capture("perl perl/ch-1.pl 1 3"), "5\n"; -is capture("perl perl/ch-1.pl 1 4"), "6\n"; - -is capture("perl perl/ch-1.pl 2 0"), "3\n"; -is capture("perl perl/ch-1.pl 2 1"), "5\n"; -is capture("perl perl/ch-1.pl 2 2"), "7\n"; -is capture("perl perl/ch-1.pl 2 3"), "9\n"; -is capture("perl perl/ch-1.pl 2 4"), "11\n"; - -is capture("perl perl/ch-1.pl 3 0"), "5\n"; -is capture("perl perl/ch-1.pl 3 1"), "13\n"; -is capture("perl perl/ch-1.pl 3 2"), "29\n"; -is capture("perl perl/ch-1.pl 3 3"), "61\n"; -is capture("perl perl/ch-1.pl 3 4"), "125\n"; - - -is capture('perl perl/ch-2.pl jdbc://user:password@localhost:3306/pwc?profile=true#h1'), <<END; -scheme: jdbc -userinfo: user:password -host: localhost -port: 3306 -path: /pwc -query: profile=true -fragment: h1 -END - -is capture('perl perl/ch-2.pl jdbc://localhost:3306/pwc?profile=true#h1'), <<END; -scheme: jdbc -userinfo: -host: localhost -port: 3306 -path: /pwc -query: profile=true -fragment: h1 -END - -is capture('perl perl/ch-2.pl jdbc://localhost/pwc?profile=true#h1'), <<END; -scheme: jdbc -userinfo: -host: localhost -port: -path: /pwc -query: profile=true -fragment: h1 -END - -is capture('perl perl/ch-2.pl jdbc:/pwc?profile=true#h1'), <<END; -scheme: jdbc -userinfo: -host: -port: -path: /pwc -query: profile=true -fragment: h1 -END - -is capture('perl perl/ch-2.pl jdbc:/pwc?profile=true'), <<END; -scheme: jdbc -userinfo: -host: -port: -path: /pwc -query: profile=true -fragment: -END - -is capture('perl perl/ch-2.pl jdbc:/pwc'), <<END; -scheme: jdbc -userinfo: -host: -port: -path: /pwc -query: -fragment: -END - -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} |
