diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-04-09 17:01:32 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-04-09 17:01:32 -0400 |
| commit | 77b38f1bc2f1c0ed6875e2ef150d4077f1109c4f (patch) | |
| tree | 0be11319aad2d8bca138affe5f960dc2eba30ea4 | |
| parent | 5991138e306a72597a1609d1cc2e41da6369c93a (diff) | |
| download | perlweeklychallenge-club-77b38f1bc2f1c0ed6875e2ef150d4077f1109c4f.tar.gz perlweeklychallenge-club-77b38f1bc2f1c0ed6875e2ef150d4077f1109c4f.tar.bz2 perlweeklychallenge-club-77b38f1bc2f1c0ed6875e2ef150d4077f1109c4f.zip | |
DAJ 264 Sun is Eclipsed By The Moon...
| -rw-r--r-- | challenge-264/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-264/dave-jacoby/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-264/dave-jacoby/perl/ch-2.pl | 40 |
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-264/dave-jacoby/blog.txt b/challenge-264/dave-jacoby/blog.txt new file mode 100644 index 0000000000..d276eaeea9 --- /dev/null +++ b/challenge-264/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2024/04/09/everything-under-the-sun-is-in-tune-weekly-challenge-264.html diff --git a/challenge-264/dave-jacoby/perl/ch-1.pl b/challenge-264/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..a1f352ffca --- /dev/null +++ b/challenge-264/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc postderef say signatures state }; + +my @examples = ( + + 'PeRlwEeKLy', + 'ChaLlenge', + 'The', +); + +for my $example (@examples) { + my $output = greatest_letter($example); + say <<"END"; + Input: \$str = "$example" + Output: "$output" +END +} + +sub greatest_letter ($str) { + my @letters = split //, $str; + my %hash; + for my $l (@letters) { + my $L = uc $l; + $hash{$L} ||= ''; + if ( $L eq uc $l ) { + if ( $L eq $l ) { # uppercase + if ( $hash{$L} !~ /U/ ) { + $hash{$L} .= 'U'; + } + } + else { # lowercase + if ( $hash{$L} !~ /L/ ) { + $hash{$L} .= 'L'; + } + } + } + } + my @contenders = + grep { length $hash{$_} == 2 } + sort { $b cmp $a } + keys %hash; + return @contenders ? shift @contenders : ''; +} diff --git a/challenge-264/dave-jacoby/perl/ch-2.pl b/challenge-264/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..4dee27fb81 --- /dev/null +++ b/challenge-264/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + { source => [ 0, 1, 2, 3, 4 ], indices => [ 0, 1, 2, 2, 1 ] }, + { source => [ 1, 2, 3, 4, 0 ], indices => [ 0, 1, 2, 3, 0 ] }, + { source => [1], indices => [0] }, + { source => [ 9, 0, 1, 2, 5 ], indices => [ 0, 1, 2, 1, 2 ] }, +); + +for my $example (@examples) { + my @output = target_array($example); + my $output = join ', ', @output; + my $source = join ', ', $example->{source}->@*; + my $indices = join ', ', $example->{indices}->@*; + + say <<"END"; + Input: \@source = ($source) + \@indices = ($indices) + Output: [ $output ] +END +} + +sub target_array ($obj) { + my @source = $obj->{source}->@*; + my @indices = $obj->{indices}->@*; + my @target; + for my $k ( 0 .. $#source ) { + my $s = $source[$k]; + my $i = $indices[$k]; + my @input = ($s); + push @input, $target[$i] + if defined $target[$i]; + splice( @target, $i, 1, @input ); + } + return @target; +} |
