diff options
| -rw-r--r-- | challenge-279/dave-jacoby/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-279/dave-jacoby/perl/ch-2.pl | 39 | ||||
| -rw-r--r-- | challenge-280/dave-jacoby/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-280/dave-jacoby/perl/ch-2.pl | 37 |
4 files changed, 153 insertions, 0 deletions
diff --git a/challenge-279/dave-jacoby/perl/ch-1.pl b/challenge-279/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..df3b55d3f9 --- /dev/null +++ b/challenge-279/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +use List::Util qw{max}; + +my @examples = ( + { + letters => [ 'R', 'E', 'P', 'L' ], + weights => [ 3, 2, 1, 4 ], + }, + { + letters => [ 'A', 'U', 'R', 'K' ], + weights => [ 2, 4, 1, 3 ], + }, + { + letters => [ 'O', 'H', 'Y', 'N', 'P', 'T' ], + weights => [ 5, 4, 2, 6, 1, 3 ], + } +); + +for my $example (@examples) { + my $output = sort_letters($example); + my $letters = join ', ', map { qq{'$_'} } $example->{letters}->@*; + my $weights = join ', ', $example->{weights}->@*; + say <<"END"; + Input: \@letters = ($letters) + \@weights = ($weights) + Output: $output +END +} + +sub sort_letters ($input) { + my @output; + for my $i ( 0..-1+scalar $input->{weights}->@*){ + my $w = $input->{weights}[$i]; + my $l = $input->{letters}[$i]; + @output[$w]= $l; + } + my $output = join '', grep { defined } @output; + return $output; +} diff --git a/challenge-279/dave-jacoby/perl/ch-2.pl b/challenge-279/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..92e8a63a8e --- /dev/null +++ b/challenge-279/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( + + 'perl', + 'book', + 'good morning', +); + +for my $input (@examples) { + my $output = split_string($input) ? 'true' : 'false'; + + say <<"END"; + Input: \$str = "$input" + Output: $output +END +} + +sub split_string ($word) { + my $l = length $word; + for my $i ( 1 .. $l ) { + my $first = substr $word, 0, $i; + my $second = substr $word, $i, $l - $i; + my $ff = scalar grep { is_vowel($_) } split //, $first; + my $ss = scalar grep { is_vowel($_) } split //, $second; + return 1 if $ff == $ss; + } + return 0; +} + +sub is_vowel ($letter) { + my @vowels = qw{a e i o u}; + return 1 if grep { $_ eq $letter } @vowels; + return 0; +} 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; +} + |
