From 130cd83b310c0957df6fe98ffc2600be5cc79fb1 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Wed, 9 Oct 2019 00:41:51 +0200 Subject: Add solutions to 029 (Brace expansion and C fucntion) by E. Choroba --- challenge-029/e-choroba/perl5/ch-1.pl | 39 +++++++++++++++++++++++++++++++++++ challenge-029/e-choroba/perl5/ch-2.pl | 23 +++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100755 challenge-029/e-choroba/perl5/ch-1.pl create mode 100755 challenge-029/e-choroba/perl5/ch-2.pl diff --git a/challenge-029/e-choroba/perl5/ch-1.pl b/challenge-029/e-choroba/perl5/ch-1.pl new file mode 100755 index 0000000000..c7de003078 --- /dev/null +++ b/challenge-029/e-choroba/perl5/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +sub expand { + my ($string) = @_; + grep length, glob $string =~ s/(\s)/\\$1/gr +} + +use Test::More tests => 5; +use Test::Deep; + +cmp_deeply [expand('Perl {Daily,Weekly,Monthly,Yearly} Challenge')], + bag('Perl Daily Challenge', + 'Perl Weekly Challenge', + 'Perl Monthly Challenge', + 'Perl Yearly Challenge'), + 'Original example'; + +cmp_deeply [expand('{a,,}')], + bag(qw( a )), + 'Empty results excluded'; + +cmp_deeply [expand('a{b,c}{d,e}f{g,h,i}')], + bag('abdfg', 'abdfh', 'abdfi', + 'abefg', 'abefh', 'abefi', + 'acdfg', 'acdfh', 'acdfi', + 'acefg', 'acefh', 'acefi'), + 'Multiple brackets'; + +cmp_deeply [expand('{{a,b}c,d}e')], + bag(qw( ace bce de )), + 'Nested brackets'; + +cmp_deeply [expand('{{a,b}c,d}{g,}')], + bag(qw( acg ac bcg bc dg d )), + 'Everything combined'; + diff --git a/challenge-029/e-choroba/perl5/ch-2.pl b/challenge-029/e-choroba/perl5/ch-2.pl new file mode 100755 index 0000000000..ed756c8f2d --- /dev/null +++ b/challenge-029/e-choroba/perl5/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Inline 'C'; + +use Test::More tests => 2; + +is factorial(20), 2432902008176640000; +is greet('world'), 'Hello world!'; + +__END__ +__C__ + +long factorial (long n) { + long r = 1; + for (long i = 2; i <= n; i++) r *= i; + return r; +} + +SV *greet (SV* name) { + return(newSVpvf("Hello %s!", SvPV(name, PL_na))); +} -- cgit