From 0d6c3fc5a2721c34fd3f9c1edb0e4b7fd8765661 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 11 Mar 2024 10:18:25 +0100 Subject: Solve 260: Unique Occurrences & Dictionary Rank by E. Choroba --- challenge-260/e-choroba/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-260/e-choroba/perl/ch-2.pl | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100755 challenge-260/e-choroba/perl/ch-1.pl create mode 100755 challenge-260/e-choroba/perl/ch-2.pl 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'; -- cgit