diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-23 15:00:37 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-07-23 15:00:37 -0400 |
| commit | 1416e39e9b81efb510892d22e3b3aaee5045e035 (patch) | |
| tree | 8ce78a78ddbfa7f6458e7b9c74090dcf589846e4 | |
| parent | 7f5df092b84ed9b8126a7f3ab334adc2a54cf63d (diff) | |
| download | perlweeklychallenge-club-1416e39e9b81efb510892d22e3b3aaee5045e035.tar.gz perlweeklychallenge-club-1416e39e9b81efb510892d22e3b3aaee5045e035.tar.bz2 perlweeklychallenge-club-1416e39e9b81efb510892d22e3b3aaee5045e035.zip | |
279 DAJ
| -rw-r--r-- | challenge-279/dave-jacoby/perl/ch-1.pl | 44 | ||||
| -rw-r--r-- | challenge-279/dave-jacoby/perl/ch-2.pl | 39 |
2 files changed, 83 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; +} |
