aboutsummaryrefslogtreecommitdiff
path: root/challenge-255
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-255')
-rw-r--r--challenge-255/laurent-rosenfeld/blog1.txt1
-rw-r--r--challenge-255/laurent-rosenfeld/perl/ch-2.pl20
-rw-r--r--challenge-255/laurent-rosenfeld/raku/ch-2.raku13
3 files changed, 34 insertions, 0 deletions
diff --git a/challenge-255/laurent-rosenfeld/blog1.txt b/challenge-255/laurent-rosenfeld/blog1.txt
new file mode 100644
index 0000000000..2ca23f5ca4
--- /dev/null
+++ b/challenge-255/laurent-rosenfeld/blog1.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/laurent_r/2024/02/perl-weekly-challenge-255-most-frequent-word.html
diff --git a/challenge-255/laurent-rosenfeld/perl/ch-2.pl b/challenge-255/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..6bd33c4c02
--- /dev/null
+++ b/challenge-255/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub most_frequent_word {
+ my ($para, $banned) = @_;
+ $para =~ tr/,.:;?!//;
+ my %histo;
+ %histo = map { $_ => ++$histo{$_} }
+ grep {$_ ne $banned} split /\W/, $para;
+ return (sort { $histo{$b} <=> $histo{$a} } keys %histo )[0];
+}
+
+my $t = "Joe hit a ball, the hit ball flew far after it was hit.";
+printf "%-30s... => ", substr $t, 0, 28;
+say most_frequent_word $t, "hit";
+
+$t = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.";
+printf "%-30s... => ", substr $t, 0, 28;
+say most_frequent_word $t, "the";
diff --git a/challenge-255/laurent-rosenfeld/raku/ch-2.raku b/challenge-255/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..98549c9386
--- /dev/null
+++ b/challenge-255/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,13 @@
+sub most-frequent-word ($para is copy, $banned) {
+ $para ~~ tr/,.:;?!//;
+ my $histo = $para.words.grep({$_ ne $banned}).Bag;
+ return $histo.keys.max({$histo{$_}});
+}
+
+my $t = "Joe hit a ball, the hit ball flew far after it was hit.";
+printf "%-30s... => ", substr $t, 0, 28;
+say most-frequent-word $t, "hit";
+
+$t = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.";
+printf "%-30s... => ", substr $t, 0, 28;
+say most-frequent-word $t, "the";