aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/jeanluc2020/perl/ch-2.pl
blob: b3896b9710129eea4e7df4bb09a797b051c7b752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env perl
# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/#TASK2
#
# Task 2: Most Frequent Word
# ==========================
#
# You are given a paragraph $p and a banned word $w.
#
# Write a script to return the most frequent word that is not banned.
#
## Example 1
##
## Input: $p = "Joe hit a ball, the hit ball flew far after it was hit."
##        $w = "hit"
## Output: "ball"
##
## The banned word "hit" occurs 3 times.
## The other word "ball" occurs 2 times.
#
## Example 2
##
## Input: $p = "Perl and Raku belong to the same family. Perl is the most
## popular language in the weekly challenge."
##        $w = "the"
## Output: "Perl"
##
## The banned word "the" occurs 3 times.
## The other word "Perl" occurs 2 times.
#
############################################################
##
## discussion
##
############################################################
#
# Split the sentence into its words, then count all the words != $w

use strict;
use warnings;

most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit");
most_frequent_word("Perl and Raku belong to the same family. Perl is the most" .
   " popular language in the weekly challenge.", "the");

sub most_frequent_word {
   my ($p, $w) = @_;
   print "Input: '$p', '$w'\n";
   my $found = {};
   foreach my $word (split/[^\w]/, $p) {
      next if $word eq $w;
      $found->{$word}++;
   }
   my @most = sort { $found->{$b} <=> $found->{$a} } keys %$found;
   print "Found the most frequent word to be '$most[0]'\n";
}