diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-11 11:04:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-11 11:04:48 +0000 |
| commit | b555f7d86b4cc0f38c935a7284549fa0feae24a7 (patch) | |
| tree | 511095f2857b2ef5d60b3457a988e371de81d18b | |
| parent | fbe04a4d671b187a73e39762c40a00441c390195 (diff) | |
| parent | 0d6c3fc5a2721c34fd3f9c1edb0e4b7fd8765661 (diff) | |
| download | perlweeklychallenge-club-b555f7d86b4cc0f38c935a7284549fa0feae24a7.tar.gz perlweeklychallenge-club-b555f7d86b4cc0f38c935a7284549fa0feae24a7.tar.bz2 perlweeklychallenge-club-b555f7d86b4cc0f38c935a7284549fa0feae24a7.zip | |
Merge pull request #9728 from choroba/ech260
Solve 260: Unique Occurrences & Dictionary Rank by E. Choroba
| -rwxr-xr-x | challenge-260/e-choroba/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-260/e-choroba/perl/ch-2.pl | 20 |
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-260/e-choroba/perl/ch-1.pl b/challenge-260/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..d1f879cdba --- /dev/null +++ b/challenge-260/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub unique_occurrences(@ints) { + my %count; + ++$count{$_} for @ints; + my %occ; + for (values %count) { + return 0 if $occ{$_}++; + } + return 1 +} + +use Test::More tests => 3 + 1; + +is unique_occurrences(1, 2, 2, 1, 1, 3), 1, 'Example 1'; +is unique_occurrences(1, 2, 3), 0, 'Example 2'; +is unique_occurrences(-2, 0, 1, -2, 1, 1, 0, 1, -2, 9), 1, 'Example 3'; + +is unique_occurrences(), 1, 'Empty'; diff --git a/challenge-260/e-choroba/perl/ch-2.pl b/challenge-260/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..0d84224d74 --- /dev/null +++ b/challenge-260/e-choroba/perl/ch-2.pl @@ -0,0 +1,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'; |
