aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpme <hauptadler@gmail.com>2024-03-11 13:40:00 +0100
committerpme <hauptadler@gmail.com>2024-03-11 13:40:00 +0100
commit121192e4cd0c1b03716a7d4920c7210dc8a2d5a7 (patch)
treec58785d259f9b8f31d9662d9f7c63648e996dffd
parentb555f7d86b4cc0f38c935a7284549fa0feae24a7 (diff)
downloadperlweeklychallenge-club-121192e4cd0c1b03716a7d4920c7210dc8a2d5a7.tar.gz
perlweeklychallenge-club-121192e4cd0c1b03716a7d4920c7210dc8a2d5a7.tar.bz2
perlweeklychallenge-club-121192e4cd0c1b03716a7d4920c7210dc8a2d5a7.zip
challenge-260
-rwxr-xr-xchallenge-260/peter-meszaros/perl/ch-1.pl56
-rwxr-xr-xchallenge-260/peter-meszaros/perl/ch-2.pl62
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-260/peter-meszaros/perl/ch-1.pl b/challenge-260/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..724c843ed6
--- /dev/null
+++ b/challenge-260/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to return 1 if the number of occurrences of each value in the
+# given array is unique or 0 otherwise.
+# Example 1
+#
+# Input: @ints = (1,2,2,1,1,3)
+# Output: 1
+#
+# The number 1 occurred 3 times.
+# The number 2 occurred 2 times.
+# The number 3 occurred 1 time.
+#
+# All occurrences are unique, therefore the output is 1.
+#
+# Example 2
+#
+# Input: @ints = (1,2,3)
+# Output: 0
+#
+# Example 3
+#
+# Input: @ints = (-2,0,1,-2,1,1,0,1,-2,9)
+# Output: 1
+#
+#
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use List::Util qw/uniqint/;
+
+my $cases = [
+ [1, 2, 2, 1, 1, 3],
+ [1, 2, 3],
+ [-2, 0, 1, -2, 1, 1, 0, 1, -2, 9],
+];
+
+sub unique_occurences
+{
+ my $l = shift;
+
+ my %h;
+ ++$h{$_} for @$l;
+
+ return (uniqint values %h) == (keys %h) ? 1 : 0;
+}
+
+is(unique_occurences($cases->[0]), 1, 'Example 1');
+is(unique_occurences($cases->[1]), 0, 'Example 2');
+is(unique_occurences($cases->[2]), 1, 'Example 3');
+done_testing();
+
+exit 0;
diff --git a/challenge-260/peter-meszaros/perl/ch-2.pl b/challenge-260/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..c98a988963
--- /dev/null
+++ b/challenge-260/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+#
+# You are given a word, $word.
+#
+# Write a script to compute the dictionary rank of the given word.
+# Example 1
+#
+# Input: $word = 'CAT'
+# Output: 3
+#
+# All possible combinations of the letters:
+# CAT, CTA, ATC, TCA, ACT, TAC
+#
+# Arrange them in alphabetical order:
+# ACT, ATC, CAT, CTA, TAC, TCA
+#
+# CAT is the 3rd in the list.
+# Therefore the dictionary rank of CAT is 3.
+#
+# Example 2
+#
+# Input: $word = 'GOOGLE'
+# Output: 88
+#
+# Example 3
+#
+# Input: $word = 'SECRET'
+# Output: 255
+#
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use Algorithm::Combinatorics qw/permutations/;
+use List::Util qw/uniqstr/;
+
+my $cases = [
+ 'CAT',
+ 'GOOGLE',
+ 'SECRET',
+];
+
+sub dictionary_rank
+{
+ my $w = shift;
+
+ my @l = permutations([split(//, $w)]);
+ @l = uniqstr sort map {join('', @$_)} @l;
+ for (0..$#l) {
+ return $_+1 if $l[$_] eq $w;
+ }
+ return undef;
+}
+
+is(dictionary_rank($cases->[0]), 3, 'Example 1');
+is(dictionary_rank($cases->[1]), 88, 'Example 2');
+is(dictionary_rank($cases->[2]), 255, 'Example 3');
+done_testing();
+
+exit 0;
+