aboutsummaryrefslogtreecommitdiff
path: root/challenge-260/e-choroba/perl/ch-2.pl
blob: 0d84224d74a62e6de781cb8679223f53b1f72327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/perl
use warnings;
use strict;
use experimental qw( signatures );

use Algorithm::Combinatorics qw{ permutations };
use List::Util qw{ uniq };

sub dictionary_rank($word) {
    my @words = uniq(map join("", @$_), permutations([sort split //, $word]));
    for my $i (0 .. $#words) {
        return $i + 1 if $words[$i] eq $word;
    }
}

use Test::More tests => 3;

is dictionary_rank('CAT'), 3, 'Example 1';
is dictionary_rank('GOOGLE'), 88, 'Example 2';
is dictionary_rank('SECRET'), 255, 'Example 3';