aboutsummaryrefslogtreecommitdiff
path: root/challenge-255/jo-37/perl/ch-2.pl
blob: 34336aeabfaca47878967726a4d038e49a3a141c (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
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -s

use v5.24;
use Test2::V0;
use List::UtilsBy 'max_by';

our ($tests, $examples);

run_tests() if $tests || $examples;	# does not return

die <<EOS unless @ARGV > 1;
usage: $0 [-examples] [-tests] [P W...]

-examples
    run the examples from the challenge
 
-tests
    run some tests

P
    a paragraph

W...
    list of banned words

EOS


### Input and Output

say mfw(@ARGV);


### Implementation

sub mfw  {
    my $p = shift;
    my %count;
    $count{$_}++ for split /\W+/, $p;
    delete @count{@_}; 
    max_by {$count{$_}} keys %count;
}


### Examples and tests

sub run_tests {
    SKIP: {
        skip "examples" unless $examples;

        is mfw('Joe hit a ball, the hit ball flew far after it was hit.',
            'hit'), 'ball', 'example 1';
        is mfw('Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.',
            'the'), 'Perl', 'example 2';
    }

    SKIP: {
        skip "tests" unless $tests;

        is mfw('one two three four five two three four five three four five four five five', 'two', 'four', 'five'), 'three', 'more banned words';
	}

    done_testing;
    exit;
}