From 57e043e9341507e7d495162dc731f4cc6605c264 Mon Sep 17 00:00:00 2001 From: Noud Date: Mon, 15 Jul 2019 23:03:26 +0200 Subject: Solutions for challenge 017 problem 1 and 2 in Perl 6 by Noud --- challenge-017/noud/perl6/ch-1.p6 | 31 +++++++++++++++++++++++++++++++ challenge-017/noud/perl6/ch-2.p6 | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-017/noud/perl6/ch-1.p6 create mode 100644 challenge-017/noud/perl6/ch-2.p6 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 = ; + 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" + }; +} + -- cgit