diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-19 16:43:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-19 16:43:06 +0100 |
| commit | 7f4fcd00f2725ad0c4e4a2e5a95d032fabc1f4c7 (patch) | |
| tree | 5f0796dbf5c0180602dbb0de87e9ae63cced0ac0 | |
| parent | 0a13f4d2e9bfd090b38d831909ffcccf5c7fd415 (diff) | |
| parent | 57e043e9341507e7d495162dc731f4cc6605c264 (diff) | |
| download | perlweeklychallenge-club-7f4fcd00f2725ad0c4e4a2e5a95d032fabc1f4c7.tar.gz perlweeklychallenge-club-7f4fcd00f2725ad0c4e4a2e5a95d032fabc1f4c7.tar.bz2 perlweeklychallenge-club-7f4fcd00f2725ad0c4e4a2e5a95d032fabc1f4c7.zip | |
Merge pull request #392 from noudald/challenge-017-noud
Solutions for challenge 017 problem 1 and 2 in Perl 6 by Noud
| -rw-r--r-- | challenge-017/noud/perl6/ch-1.p6 | 31 | ||||
| -rw-r--r-- | challenge-017/noud/perl6/ch-2.p6 | 37 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-017/noud/perl6/ch-1.p6 b/challenge-017/noud/perl6/ch-1.p6 new file mode 100644 index 0000000000..076e7deec1 --- /dev/null +++ b/challenge-017/noud/perl6/ch-1.p6 @@ -0,0 +1,31 @@ +# 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 + +multi A(0, Int $n) { $n + 1 }; +multi A(Int $m, 0) { A($m - 1, 1) }; +multi A(Int $m, Int $n) { A($m - 1, A($m, $n - 1)) }; + + +multi MAIN('test') { + use Test; + + # As is known, the Ackerman function grows fast. Hence we only test the + # Ackermann function for some small values. + is A(1, 2), 4; + is A(1, 1), 3; + is A(2, 2), 7; + is A(3, 3), 61; +} diff --git a/challenge-017/noud/perl6/ch-2.p6 b/challenge-017/noud/perl6/ch-2.p6 new file mode 100644 index 0000000000..753390e8bf --- /dev/null +++ b/challenge-017/noud/perl6/ch-2.p6 @@ -0,0 +1,37 @@ +# 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: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 + +sub parse($url) { + my @keys = <scheme userinfo host port path query fragment>; + my $reg = / (\w+\:\w+)\:\/\/(\w+\:.+)\@(\w+)\:(\d+)(\/\w+)\?(.+)\#(\w+)$ /; + + return Hash.new(@keys Z ($url ~~ $reg).values); +} + + +multi MAIN('test') { + use Test; + + is parse("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" + }; +} + |
