perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 370 - Task 1: Popular Word

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-370/#TASK1
  3 #
  4 # Task 1: Popular Word
  5 # ====================
  6 #
  7 # You are given a string paragraph and an array of the banned words.
  8 #
  9 # Write a script to return the most popular word that is not banned. It is
 10 # guaranteed there is at least one word that is not banned and the answer is
 11 # unique. The words in paragraph are case-insensitive and the answer should be
 12 # in lowercase. The words can not contain punctuation symbols.
 13 #
 14 ## Example 1
 15 ##
 16 ## Input: $paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
 17 ##        @banned = ("hit")
 18 ## Output: "ball"
 19 ##
 20 ## After removing punctuation and converting to lowercase, the word "hit"
 21 ## appears 3 times, and "ball" appears 2 times.
 22 ## Since "hit" is on the banned list, we ignore it.
 23 #
 24 ## Example 2
 25 ##
 26 ## Input: $paragraph = "Apple? apple! Apple, pear, orange, pear, apple, orange."
 27 ##        @banned = ("apple", "pear")
 28 ## Output: "orange"
 29 ##
 30 ## "apple"  appears 4 times.
 31 ## "pear"   appears 2 times.
 32 ## "orange" appears 2 times.
 33 ##
 34 ## "apple" and "pear" are both banned.
 35 ## Even though "orange" has the same frequency as "pear", it is the only
 36 ## non-banned word with the highest frequency.
 37 #
 38 ## Example 3
 39 ##
 40 ## Input: $paragraph = "A. a, a! A. B. b. b."
 41 ##        @banned = ("b")
 42 ## Output: "a"
 43 ##
 44 ## "a" appears 4 times.
 45 ## "b" appears 3 times.
 46 ##
 47 ## The input has mixed casing and heavy punctuation.
 48 ## The normalised, "a" is the clear winner, since "b" is banned, "a" is the only
 49 ## choice.
 50 #
 51 ## Example 4
 52 ##
 53 ## Input: $paragraph = "Ball.ball,ball:apple!apple.banana"
 54 ##        @banned = ("ball")
 55 ## Output: "apple"
 56 ##
 57 ## Here the punctuation acts as a delimiter.
 58 ## "ball"   appears 3 times.
 59 ## "apple"  appears 2 times.
 60 ## "banana" appears 1 time.
 61 #
 62 ## Example 5
 63 ##
 64 ## Input: $paragraph = "The dog chased the cat, but the dog was faster than the cat."
 65 ##        @banned = ("the", "dog")
 66 ## Output: "cat"
 67 ##
 68 ## "the" appears 4 times.
 69 ## "dog" appears 2 times.
 70 ## "cat" appears 2 times.
 71 ##
 72 ## "chased", "but", "was", "faster", "than" appear 1 time each.
 73 ## "the" is the most frequent but is banned.
 74 ## "dog" is the next most frequent but is also banned.
 75 ## The next most frequent non-banned word is "cat".
 76 #
 77 ############################################################
 78 ##
 79 ## discussion
 80 ##
 81 ############################################################
 82 #
 83 # We split the sentence into its word by splitting at non-word characters.
 84 # Then we check for each word whether it is in @banned - if it isn't, we
 85 # count the word. In the end we sort the different words by count and pick
 86 # the first element of that sorted list as the result.
 87 
 88 use v5.36;
 89 
 90 scramble_string("Bob hit a ball, the hit BALL flew far after it was hit.", ("hit"));
 91 scramble_string("Apple? apple! Apple, pear, orange, pear, apple, orange.", ("apple", "pear"));
 92 scramble_string("A. a, a! A. B. b. b.", ("b"));
 93 scramble_string("Ball.ball,ball:apple!apple.banana", ("ball"));
 94 scramble_string("The dog chased the cat, but the dog was faster than the cat.", ("the", "dog"));
 95 
 96 +-- 15 lines: sub scramble_string($paragraph, @banned) {----------------------------------------------------------------------------------------------------------------------------
111