From 0963991c500b8bee7bda431453859905db9c3922 Mon Sep 17 00:00:00 2001 From: Ysmael Ebreo Date: Sat, 12 Oct 2019 02:54:13 +0800 Subject: Added perl6 solutions for ch#29-1 and 2 --- challenge-029/yet-ebreo/perl5/ch-1.pl | 2 +- challenge-029/yet-ebreo/perl5/ch-2.pl | 4 +++- challenge-029/yet-ebreo/perl6/ch-1.p6 | 43 ++++++++++++++++++++++++++++++++++ challenge-029/yet-ebreo/perl6/ch-2.p6 | 36 ++++++++++++++++++++++++++++ challenge-029/yet-ebreo/perl6/fib.so | Bin 0 -> 48344 bytes 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 challenge-029/yet-ebreo/perl6/ch-1.p6 create mode 100644 challenge-029/yet-ebreo/perl6/ch-2.p6 create mode 100644 challenge-029/yet-ebreo/perl6/fib.so diff --git a/challenge-029/yet-ebreo/perl5/ch-1.pl b/challenge-029/yet-ebreo/perl5/ch-1.pl index a0392cc0d0..46d8fc7865 100644 --- a/challenge-029/yet-ebreo/perl5/ch-1.pl +++ b/challenge-029/yet-ebreo/perl5/ch-1.pl @@ -36,7 +36,7 @@ sub expand { my ($l,$m,$r) = ($`,$1,$'); # The captured value stored in $m was split using comma(,) as delimiter # The resulting list was used in a for loop - for (split ",", $m) { + for (",$m"=~/,([^,]*)/g) { #A new string containing the prematch, a value from the split operation of $m #and the postmatch will be used in the recursive function. #The process will be repeated until... diff --git a/challenge-029/yet-ebreo/perl5/ch-2.pl b/challenge-029/yet-ebreo/perl5/ch-2.pl index fbb28cf5d8..4c8b959c50 100644 --- a/challenge-029/yet-ebreo/perl5/ch-2.pl +++ b/challenge-029/yet-ebreo/perl5/ch-2.pl @@ -1,4 +1,6 @@ - +#!/usr/bin/env perl +# Write a script to demonstrate calling a C function. +# It could be any user defined or standard C function. use strict; use warnings; use feature 'say'; diff --git a/challenge-029/yet-ebreo/perl6/ch-1.p6 b/challenge-029/yet-ebreo/perl6/ch-1.p6 new file mode 100644 index 0000000000..71d2c26c6a --- /dev/null +++ b/challenge-029/yet-ebreo/perl6/ch-1.p6 @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +# Write a script to demonstrate brace expansion. +# For example, script would take command line argument +# Perl {Daily,Weekly,Monthly,Yearly} Challenge and should expand it and print like below: +# Perl Daily Challenge +# Perl Weekly Challenge +# Perl Monthly Challenge +# Perl Yearly Challenge + +sub MAIN ( + $string #= String containing braces "{}" to expand +) { + my $mstring = $string; + $mstring~~s:g/\{\}//; + expand($mstring); +} + +sub expand ($string) { + #The string will be stored in $string + my $mstring = $string; + + #- Regex was used to check if the string contains matching braces + #- Notice that [^{}]* instead of a simple .* to match the contents of the braces + # this is to make sure that the inner most brace is processed first + #- The matching string was captured using () and will be stored in $1 + if ($mstring ~~ /\{(<-[{}]>*)\}/) { + # The captured value, the prematch and postmatch were stored + # in variables $l,$m and $r respectively + my ($l,$m,$r) = ($/.prematch,$0,$/.postmatch); + # The captured value stored in $m was split using comma(,) as delimiter + # The resulting list was used in a for loop + for ($m.split(",")) { + #A new string containing the prematch, a value from the split operation of $m + #and the postmatch will be used in the recursive function. + #The process will be repeated until... + expand($l~$_~$r); + } + } else { + #The string does not have matching braces. + #Then final string will be printed + say $mstring; + } +} \ No newline at end of file diff --git a/challenge-029/yet-ebreo/perl6/ch-2.p6 b/challenge-029/yet-ebreo/perl6/ch-2.p6 new file mode 100644 index 0000000000..867c17daa2 --- /dev/null +++ b/challenge-029/yet-ebreo/perl6/ch-2.p6 @@ -0,0 +1,36 @@ +use NativeCall; +use experimental :cached; +sub fib(int32) returns int32 is native('fib.so') {*} + +sub MAIN () { + + my $start = now; + print (perl_fib($_)~" ") for 1..36; + + say "\nRun Time (Perl): "~(now - $start)~" sec\n"; + + $start = now; + print (perl_fib_cached($_)~" ") for 1..36; + + say "\nRun Time (Perl-Cached): "~(now - $start)~" sec\n"; + + $start = now; + print (fib($_)~" ") for 1..36; + + say "\nRun Time: (NativeCall)"~(now - $start)~" sec\n"; +} + +sub perl_fib($n) { ($n == 0) ?? 0 !! ($n == 1) ?? 1 !! perl_fib($n-1)+perl_fib($n-2) } +sub perl_fib_cached($n) is cached { ($n == 0) ?? 0 !! ($n == 1) ?? 1 !! perl_fib_cached($n-1)+perl_fib_cached($n-2) } + + +# perl6 .\ch-2.p6 32 +# 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 +# Run Time (Perl): 74.794843 sec +# +# 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 +# Run Time (Perl-Cached): 0.029755 sec +# +# 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 +# Run Time: (NativeCall)0.31090124 sec + diff --git a/challenge-029/yet-ebreo/perl6/fib.so b/challenge-029/yet-ebreo/perl6/fib.so new file mode 100644 index 0000000000..95cef59a3c Binary files /dev/null and b/challenge-029/yet-ebreo/perl6/fib.so differ -- cgit