aboutsummaryrefslogtreecommitdiff
path: root/challenge-255
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2024-02-07 13:39:57 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2024-02-07 13:39:57 +0000
commit67a68c401ffc44ea5d74d100dd971b3a80dccc2f (patch)
tree75370530d79d110532af5c9dba5ff2cf5118d32f /challenge-255
parent1a1891f44a2067d08ab9ec108eaccd55b789a651 (diff)
downloadperlweeklychallenge-club-67a68c401ffc44ea5d74d100dd971b3a80dccc2f.tar.gz
perlweeklychallenge-club-67a68c401ffc44ea5d74d100dd971b3a80dccc2f.tar.bz2
perlweeklychallenge-club-67a68c401ffc44ea5d74d100dd971b3a80dccc2f.zip
- Added solutions by Stephen G Lynn.
- Added solutions by Roger Bell_West. - Added solutions by Laurent Rosenfeld.
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";