diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-29 10:07:54 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-29 10:07:54 -0400 |
| commit | 81f635a0cf3d07e3d5115eb9977e97f4ec7fcfa8 (patch) | |
| tree | add3048c781f2c8fb49e6c8c343b6355efb28359 | |
| parent | 4618fa1a2b0f570de8f9d4c1b4f3ee35b1eac24f (diff) | |
| download | perlweeklychallenge-club-81f635a0cf3d07e3d5115eb9977e97f4ec7fcfa8.tar.gz perlweeklychallenge-club-81f635a0cf3d07e3d5115eb9977e97f4ec7fcfa8.tar.bz2 perlweeklychallenge-club-81f635a0cf3d07e3d5115eb9977e97f4ec7fcfa8.zip | |
280 DAJ
| -rw-r--r-- | challenge-280/dave-jacoby/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-280/dave-jacoby/perl/ch-2.pl | 37 |
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-280/dave-jacoby/perl/ch-1.pl b/challenge-280/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2426dd92aa --- /dev/null +++ b/challenge-280/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +my @examples = ( + + qw{ + acbddbca + abccd + abcdabbb + }, +); + +for my $example (@examples) { + my $output = twice_appearance($example); + say <<"END"; + Input: \@str = "$example" + Output: "$output" +END +} + +sub twice_appearance ($input) { + my $hash; + for my $i ( split //, $input ) { + $hash->{$i}++; + return $i if $hash->{$i} > 1; + + } + + return ''; +} diff --git a/challenge-280/dave-jacoby/perl/ch-2.pl b/challenge-280/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..e4a6a683a6 --- /dev/null +++ b/challenge-280/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( + + 'p|*e*rl|w**e|*ekly|', + 'perl', + 'th|ewe|e**|k|l***ych|alleng|e', + +); + +for my $input (@examples) { + my $output = count_asterisks($input); + + say <<"END"; + Input: \$str = "$input" + Output: $output +END +} + +sub count_asterisks ($str) { + $str =~ s{ + # if we can comment a regex, we probably should + \| # a pipe character + [^\|]* # zero or more non-pip characters + \| # a pipe character + }{ }gmix; + + # the = () = forces it into a list context, and otherwise + # you'd get a boolean result. + my $c = () = $str =~ /(\*)/gmix; + return $c; +} + |
