aboutsummaryrefslogtreecommitdiff
path: root/challenge-017
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2019-07-18 08:14:58 -0300
committerandrezgz <andrezgz@gmail.com>2019-07-18 08:14:58 -0300
commit09c8652f9451b2d69764a064b8ce81c6ed90c9fd (patch)
tree4075dfe3689cf3e46311e325550f1acaff515529 /challenge-017
parentac7c70c20699f00513b8cb08240ec5f3ea1125c6 (diff)
downloadperlweeklychallenge-club-09c8652f9451b2d69764a064b8ce81c6ed90c9fd.tar.gz
perlweeklychallenge-club-09c8652f9451b2d69764a064b8ce81c6ed90c9fd.tar.bz2
perlweeklychallenge-club-09c8652f9451b2d69764a064b8ce81c6ed90c9fd.zip
challenge 017 andrezgz solutions
Diffstat (limited to 'challenge-017')
-rw-r--r--challenge-017/andrezgz/perl5/ch-1.pl37
-rw-r--r--challenge-017/andrezgz/perl5/ch-2.pl60
2 files changed, 97 insertions, 0 deletions
diff --git a/challenge-017/andrezgz/perl5/ch-1.pl b/challenge-017/andrezgz/perl5/ch-1.pl
new file mode 100644
index 0000000000..4699450e94
--- /dev/null
+++ b/challenge-017/andrezgz/perl5/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-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.
+# https://en.wikipedia.org/wiki/Ackermann_function
+# 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
+
+use strict;
+use warnings;
+no warnings 'recursion';
+#Deep recursion on subroutine "%s"
+#(W recursion) This subroutine has called itself (directly or indirectly) 100 times more than it has returned.
+#This probably indicates an infinite recursion, unless you're writing strange benchmark programs, in which case it indicates something else.
+
+use Memoize;
+memoize('a');
+
+sub a {
+ my ($m, $n) = @_;
+ return $n+1 if ($m == 0);
+ return a($m-1,1) if ($n == 0);
+ return a($m-1,a($m,$n-1));
+}
+
+die "Usage: $0 <m,n>" unless ($ARGV[0] && $ARGV[0] =~ /\d,\d/);
+print a(split /,/,$ARGV[0]);
diff --git a/challenge-017/andrezgz/perl5/ch-2.pl b/challenge-017/andrezgz/perl5/ch-2.pl
new file mode 100644
index 0000000000..ea7368f303
--- /dev/null
+++ b/challenge-017/andrezgz/perl5/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-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:
+# https://en.wikipedia.org/wiki/URL
+# scheme:[//[userinfo@]host[:port]]path[?query][#fragment]
+#
+# For example: jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1
+#
+# scheme: jdbc:mysql
+# userinfo: user:password
+# host: localhost
+# port: 3306
+# path: /pwc
+# query: profile=true
+# fragment: h1
+
+use strict;
+use warnings;
+
+my $uri = shift || 'jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1';
+
+my ($scheme, $auth, $path, $query, $frag) = uri_split($uri);
+my ($userinfo, $host, $port) = auth_split($auth) if $auth;
+
+show('scheme',$scheme);
+show('userinfo',$userinfo);
+show('host',$host);
+show('port',$port);
+show('path',$path);
+show('query',$query);
+show('fragment',$frag);
+
+#based on URI::Split
+sub uri_split {
+ return $_[0] =~ m|
+ ^ # string start
+ ([^/?\#]+) : # scheme
+ (?: // ([^/?\#]*) )? # authority (optional)
+ ([^?\#]*) # path
+ (?: \? ([^\#]*) )? # query (optional)
+ (?: \# (.*) )? # fragment (optional)
+ $ # string end
+ |x;
+}
+
+sub auth_split {
+ return $_[0] =~ m|
+ (?: ([^@]+) @ )? # userinfo (optional)
+ ([^:/?\#]+) # host
+ (?: : (\d+) )? # port (optional)
+ |x;
+}
+
+sub show {
+ my $name = shift . ':';
+ my $value = shift || '';
+ print sprintf("%-10s%s\n",$name,$value);
+}