From 67a68c401ffc44ea5d74d100dd971b3a80dccc2f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 7 Feb 2024 13:39:57 +0000 Subject: - Added solutions by Stephen G Lynn. - Added solutions by Roger Bell_West. - Added solutions by Laurent Rosenfeld. --- challenge-255/laurent-rosenfeld/blog1.txt | 1 + challenge-255/laurent-rosenfeld/perl/ch-2.pl | 20 ++++++++++++++++++++ challenge-255/laurent-rosenfeld/raku/ch-2.raku | 13 +++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 challenge-255/laurent-rosenfeld/blog1.txt create mode 100644 challenge-255/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-255/laurent-rosenfeld/raku/ch-2.raku (limited to 'challenge-255') 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"; -- cgit