diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-10-09 18:20:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-09 18:20:13 +0100 |
| commit | 13d69016cd966ba92204f2c07edbb29855b91e13 (patch) | |
| tree | 0f1ce67ecd9797445cf8ed5f34a777712f7de0ee | |
| parent | 83a9aae82c8c36205d38594bc352673058c2d404 (diff) | |
| parent | ae603a6befbef48b4a3f6fe301bd58bd1dba6e81 (diff) | |
| download | perlweeklychallenge-club-13d69016cd966ba92204f2c07edbb29855b91e13.tar.gz perlweeklychallenge-club-13d69016cd966ba92204f2c07edbb29855b91e13.tar.bz2 perlweeklychallenge-club-13d69016cd966ba92204f2c07edbb29855b91e13.zip | |
Merge pull request #737 from andrezgz/challenge-029
challenge-029 andrezgz solution
| -rw-r--r-- | challenge-029/andrezgz/perl5/ch-1.pl | 56 | ||||
| -rw-r--r-- | challenge-029/andrezgz/perl5/ch-2.pl | 24 |
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-029/andrezgz/perl5/ch-1.pl b/challenge-029/andrezgz/perl5/ch-1.pl new file mode 100644 index 0000000000..f8b04fd13b --- /dev/null +++ b/challenge-029/andrezgz/perl5/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-029/ +# Task #1 +# 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 + +use strict; +use warnings; + +sub expand { + my $input = shift; + my %expansions; + + if ($input =~ m/ (.*?) # optional preamble + \{ ([^{}]*?) \} # pair of braces that not contain braces inside + (.*) # optional postscript + /x ) { + + my ($preamble, $brace_match, $postscript) = ($1,$2,$3); + $brace_match = adjust_range($brace_match); + + foreach my $item ( split /,/, $brace_match ) { + foreach ( expand( $preamble . $item . $postscript ) ) { + $expansions{$_} = 1; #unique expansions + } + } + } + + return %expansions ? sort keys %expansions : ($input); +} + +# replace range with list of values +sub adjust_range { + my $brace_exp = shift; + return join ',', ($1..$2) if ( $brace_exp =~ /^(\d+)[.]{2}(\d+)$/ ); # numeric range n..m + return join ',', ($1..$2) if ( $brace_exp =~ /^([a-z]+)[.]{2}([a-z]+)$/i ); # alphabetic range a..z + return $brace_exp; # unmodified, no range detected +} + +while (<DATA>) { + chomp; + print "Expansion for: '$_'\n"; + print join "\n", expand($_); + print "\n\n"; +} + +__DATA__ +last{mce,boot,xorg}.log +file.{2015..2017}.{a..c}.log{,.bak} +/home/tom/{temp,tmp{1..4}}/image.{jpg,png} +Perl {{Dai,Week,Month,Year}ly,Unique} Challenge diff --git a/challenge-029/andrezgz/perl5/ch-2.pl b/challenge-029/andrezgz/perl5/ch-2.pl new file mode 100644 index 0000000000..f131713b81 --- /dev/null +++ b/challenge-029/andrezgz/perl5/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-029/ +# Task #2 +# Write a script to demonstrate calling a C function. +# It could be any user defined or standard C function. + +use strict; +use warnings; + +use Inline C => <<'EOT'; +float square ( float x ) +{ + return ( x * x ); +} +EOT + +my $input = shift || die "USAGE: $0 <number>"; +print square $input; + +__END__ + +./ch-2.pl 12 +144 |
