diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-02-05 19:14:25 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-02-05 19:14:25 -0500 |
| commit | d6115876d49d05a8fe4fe6ed37cbacdd0ed93d40 (patch) | |
| tree | 1e02ec09b54f796ff1f261defa3fb0acf3a9e8e6 /challenge-255 | |
| parent | cde7d521095de2ce582570f6d8d1a9e7bbcec16c (diff) | |
| download | perlweeklychallenge-club-d6115876d49d05a8fe4fe6ed37cbacdd0ed93d40.tar.gz perlweeklychallenge-club-d6115876d49d05a8fe4fe6ed37cbacdd0ed93d40.tar.bz2 perlweeklychallenge-club-d6115876d49d05a8fe4fe6ed37cbacdd0ed93d40.zip | |
255 DAJ
Diffstat (limited to 'challenge-255')
| -rw-r--r-- | challenge-255/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-255/dave-jacoby/perl/ch-1.pl | 53 | ||||
| -rw-r--r-- | challenge-255/dave-jacoby/perl/ch-2.pl | 50 |
3 files changed, 104 insertions, 0 deletions
diff --git a/challenge-255/dave-jacoby/blog.txt b/challenge-255/dave-jacoby/blog.txt new file mode 100644 index 0000000000..a48eef06c8 --- /dev/null +++ b/challenge-255/dave-jacoby/blog.txt @@ -0,0 +1 @@ + https://jacoby.github.io/2024/02/05/preel-weeakly-weekly-challenge-255.html diff --git a/challenge-255/dave-jacoby/perl/ch-1.pl b/challenge-255/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..332918c8f4 --- /dev/null +++ b/challenge-255/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Carp; +use List::Compare; + +my @examples = ( + + { s => "Perl", t => "Preel" }, + { s => "Weekly", t => "Weeakly" }, + { s => "Box", t => "Boxy" }, +); + +for my $example (@examples) { + my $output = odd_character($example); + my $s = $example->{s}; + my $t = $example->{t}; + + say <<~"END"; + Input: \$s = "$s" \$t = "$t" + Output: $output + END +} + +sub odd_character ($input) { + my @s = sort split //, $input->{s}; + my @t = sort split //, $input->{t}; + say join ', ', @s; + say join ', ', @t; + return 7; + my @output; + while ( @s && @t ) { + if ( $s[0] eq $t[0] ) { + shift @s; + shift @t; + } + else { + if ( scalar @s > scalar @t ) { + push @output, shift @s; + } + elsif ( scalar @s < scalar @t ) { + push @output, shift @t; + } + else { croak 'Impossible Scenario' } + } + } + push @output, @s if @s; + push @output, @t if @t; + return shift @output; +} diff --git a/challenge-255/dave-jacoby/perl/ch-2.pl b/challenge-255/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..c5bed865fa --- /dev/null +++ b/challenge-255/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max }; + +my @examples = ( + + { + paragraph => + "Joe hit a ball, the hit ball flew far after it was hit.", + word => "hit", + }, + { + paragraph => +"Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", + word => "the", + } +); + +for my $example (@examples) { + my $output = most_frequent_word($example); + my $p = $example->{paragraph}; + my $w = $example->{word}; + + say <<~"END"; + Input: \$p = "$p" + \$w = "$w" + Output: "$output" + END +} + +sub most_frequent_word ($obj) { + my $paragraph = $obj->{paragraph}; + my $banned_word = $obj->{word}; + my %hash; + + # some people REALLY hate map being used in this way, believing + # that it should end in (start with) @array = , but clearly, + # I disagree + map { $hash{$_}++ } + grep { $_ ne $banned_word } + split /\W+/, $paragraph; + my $max = max values %hash; + my @output = + grep { $hash{$_} == $max } keys %hash; + return shift @output; +} |
