From ae603a6befbef48b4a3f6fe301bd58bd1dba6e81 Mon Sep 17 00:00:00 2001 From: andrezgz Date: Wed, 9 Oct 2019 08:15:58 -0300 Subject: challenge-029 andrezgz solution --- challenge-029/andrezgz/perl5/ch-1.pl | 56 ++++++++++++++++++++++++++++++++++++ challenge-029/andrezgz/perl5/ch-2.pl | 24 ++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 challenge-029/andrezgz/perl5/ch-1.pl create mode 100644 challenge-029/andrezgz/perl5/ch-2.pl 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 () { + 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 "; +print square $input; + +__END__ + +./ch-2.pl 12 +144 -- cgit