diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-20 20:59:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-20 20:59:26 +0100 |
| commit | 3927d9f0204a4ec8a529dd9724e5954d5d59fa9e (patch) | |
| tree | 64838490bd37cd47fb671b5f733895c51e704e05 /challenge-017 | |
| parent | cf52a2d33cc8c1603626859a48edb826279aee17 (diff) | |
| parent | 040328d0b0e2e528e06ba9e7f49cbaa38697ff9e (diff) | |
| download | perlweeklychallenge-club-3927d9f0204a4ec8a529dd9724e5954d5d59fa9e.tar.gz perlweeklychallenge-club-3927d9f0204a4ec8a529dd9724e5954d5d59fa9e.tar.bz2 perlweeklychallenge-club-3927d9f0204a4ec8a529dd9724e5954d5d59fa9e.zip | |
Merge pull request #394 from seaker/mybranch
challenge 017
Diffstat (limited to 'challenge-017')
| -rwxr-xr-x | challenge-017/feng-chang/perl5/ch-1.pl | 27 | ||||
| -rwxr-xr-x | challenge-017/feng-chang/perl5/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-017/feng-chang/perl6/ch-1.p6 | 22 | ||||
| -rwxr-xr-x | challenge-017/feng-chang/perl6/ch-2.p6 | 53 |
4 files changed, 138 insertions, 0 deletions
diff --git a/challenge-017/feng-chang/perl5/ch-1.pl b/challenge-017/feng-chang/perl5/ch-1.pl new file mode 100755 index 0000000000..560139c16e --- /dev/null +++ b/challenge-017/feng-chang/perl5/ch-1.pl @@ -0,0 +1,27 @@ +#!/bin/env perl + +use Modern::Perl; + +say "A(0,$_) ", A(0, $_) for 0..9; +say "A(1,$_) ", A(1, $_) for 0..9; +say "A(2,$_) ", A(2, $_) for 0..9; +say "A(3,$_) ", A(3, $_) for 0..9; + +my @pool; + +sub A { + my ($m, $n) = @_; + die "parameter less than 0\n" if $m < 0 or $n < 0; + + return $pool[$m][$n] if defined $pool[$m][$n]; + + if ($m == 0) { + $pool[0][$n] = $n + 1; + } elsif ($n == 0) { + $pool[$m][0] = A($m - 1, 1); + } else { + $pool[$m][$n] = A($m - 1, A($m, $n - 1)); + } + + return $pool[$m][$n]; +} diff --git a/challenge-017/feng-chang/perl5/ch-2.pl b/challenge-017/feng-chang/perl5/ch-2.pl new file mode 100755 index 0000000000..a1f542e831 --- /dev/null +++ b/challenge-017/feng-chang/perl5/ch-2.pl @@ -0,0 +1,36 @@ +#!/bin/env perl + +use Modern::Perl; + +my $s = 'jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1'; + +# scheme:[//[userinfo@]host[:port]]path[?query][#fragment] +# e.g.: jdbc:mysql://user:password@localhost:3306/pwc?profile=true#h1 +if ($s =~ m/^ + (?<scheme> [a-zA-Z][-+.a-zA-Z0-9]* (?: : [a-zA-Z][-+.a-zA-Z0-9]*)? ) + : (?: \/\/ + (?: + (?<userinfo> + (?<user> [a-z][-+.a-zA-Z0-9]*) : + (?<password> [-+.a-zA-Z0-9]*) + ) + @ + )? + (?<host> [a-zA-Z0-9][-+.a-zA-Z0-9]*) + (?: : (?<port> \d+) )? + )? + (?<path> \/ (?: \w+? \/? )* ) + (?: \? (?<query> [a-zA-Z][-+.a-zA-Z0-9]* = \w+ (?: & [a-zA-Z][-+.a-zA-Z0-9]* = \w+)* ))? + (?: \# (?<fragment> [a-zA-Z][-+.a-zA-Z0-9]*))? + $/x + ) { + say 'scheme: ', $+{ scheme }; + say 'userinfo: ', $+{ userinfo } if defined $+{ userinfo }; + #say ' user: ', $+{ user } if defined $+{ user }; + #say ' password: ', $+{ password } if defined $+{ password }; + say 'host: ', $+{ host } if defined $+{ host }; + say 'port: ', $+{ port } if defined $+{ port }; + say 'path: ', $+{ path }; + say 'query: ', $+{ query } if defined $+{ query }; + say 'fragment: ', $+{ fragment } if defined $+{ fragment }; +} diff --git a/challenge-017/feng-chang/perl6/ch-1.p6 b/challenge-017/feng-chang/perl6/ch-1.p6 new file mode 100755 index 0000000000..9081f56910 --- /dev/null +++ b/challenge-017/feng-chang/perl6/ch-1.p6 @@ -0,0 +1,22 @@ +#!/bin/env perl6 + +my @pool; + +say "A(0,$_) ", A(0, $_) for 0..9; +say "A(1,$_) ", A(1, $_) for 0..9; +say "A(2,$_) ", A(2, $_) for 0..9; +say "A(3,$_) ", A(3, $_) for 0..9; + +sub A(UInt $m, UInt $n) returns UInt { + return @pool[$m][$n] if @pool[$m][$n].defined; + + if $m == 0 { + @pool[0][$n] = $n + 1; + } elsif $n == 0 { + @pool[$m][0] = A($m - 1, 1); + } else { + @pool[$m][$n] = A($m - 1, A($m, $n - 1)); + } + + return @pool[$m][$n]; +} diff --git a/challenge-017/feng-chang/perl6/ch-2.p6 b/challenge-017/feng-chang/perl6/ch-2.p6 new file mode 100755 index 0000000000..e9c140a149 --- /dev/null +++ b/challenge-017/feng-chang/perl6/ch-2.p6 @@ -0,0 +1,53 @@ +#!/bin/env perl6 + +# scheme:[//[userinfo@]host[:port]]path[?query][#fragment] +grammar URL { + token TOP { ^ + <scheme> ':' + [ '//' + [ <userinfo> '@' ]? + <host> + [ ':' <port> ]? + ]? + <path> + [ '?' <queries> ]? + [ '#' <fragment> ]? + $ } + + token scheme { <id> } + + token userinfo { <user> [ ':' <password> ]? } + token user { <id> } + token password { \w+ } + + token host { [ <IPv4-address> | <domain-name> ] } + token IPv4-address { [\d ** 1..3] ** 4 % '.' } + token dname { <[a..zA..Z]> <[-a..zA..Z0..9]>* } + token domain-name { <dname>+ % '.' } + + token port { \d+ } + + token path { '/' [<[a..zA..Z0..9]>+] *%% '/' } + + token key { <id> } + token value { \w+ } + token query { <key> '=' <value> } + token queries { <query> +% '&' } + + token fragment { <id> } + + token id { <[a..zA..Z]> <[-+.a..zA..Z0..9]>* } +} + +sub MAIN(Str $url = 'jdbc://user:password@localhost:3306/pwc?profile=true#h1') { + my $m = URL.parse($url); + if $m { + say 'scheme: ', ~$m<scheme>; + say 'userinfo: ', ~$m<userinfo> if $m<userinfo>; + say 'host: ', ~$m<host> if $m<host>; + say 'port: ', ~$m<port> if $m<port>; + say 'path: ', ~$m<path>; + say 'query: ', ~$m<queries> if $m<queries>; + say 'fragment: ', ~$m<fragment> if $m<fragment>; + } +} |
