aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-11-19 17:56:28 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-11-19 17:56:28 +0000
commitc41cbd7803555cc523ed97adcff8e7652f4a6444 (patch)
tree972b56f5c93d6d4b040abe8ceafa2fb515cd6646
parentdedcd129afc84897b659b7390209a9ec78382416 (diff)
downloadperlweeklychallenge-club-c41cbd7803555cc523ed97adcff8e7652f4a6444.tar.gz
perlweeklychallenge-club-c41cbd7803555cc523ed97adcff8e7652f4a6444.tar.bz2
perlweeklychallenge-club-c41cbd7803555cc523ed97adcff8e7652f4a6444.zip
Add Python solution to challenge 17
-rw-r--r--challenge-017/paulo-custodio/Makefile2
-rw-r--r--challenge-017/paulo-custodio/python/ch-1.py32
-rw-r--r--challenge-017/paulo-custodio/python/ch-2.py87
-rw-r--r--challenge-017/paulo-custodio/t/test-1.yaml100
-rw-r--r--challenge-017/paulo-custodio/t/test-2.yaml72
-rw-r--r--challenge-017/paulo-custodio/test.pl98
6 files changed, 293 insertions, 98 deletions
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;
-}