aboutsummaryrefslogtreecommitdiff
path: root/challenge-260
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2024-03-13 21:33:59 +0000
committerNiels van Dijke <perlboy@cpan.org>2024-03-13 21:33:59 +0000
commitf4718484b6268822dbb9bc8c5abdf2e86d78c0f1 (patch)
tree6c51332554df03656f3e86d1cf77c9ab92bff65e /challenge-260
parent227709518d98f05fca532a5b249e3ef739340c17 (diff)
downloadperlweeklychallenge-club-f4718484b6268822dbb9bc8c5abdf2e86d78c0f1.tar.gz
perlweeklychallenge-club-f4718484b6268822dbb9bc8c5abdf2e86d78c0f1.tar.bz2
perlweeklychallenge-club-f4718484b6268822dbb9bc8c5abdf2e86d78c0f1.zip
w260 - Task 1 & 2
Diffstat (limited to 'challenge-260')
-rwxr-xr-xchallenge-260/perlboy1967/perl/ch1.pl37
-rwxr-xr-xchallenge-260/perlboy1967/perl/ch2.pl37
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-260/perlboy1967/perl/ch1.pl b/challenge-260/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..06484bacc9
--- /dev/null
+++ b/challenge-260/perlboy1967/perl/ch1.pl
@@ -0,0 +1,37 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 260
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-260
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Unique Occurrences
+Submitted by: Mohammad Sajid Anwar
+
+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.
+
+=cut
+
+use v5.32;
+use feature q(signatures);
+use common::sense;
+
+use Test2::V0;
+
+use List::AllUtils qw(uniq);
+
+sub uniqueOccurences (@ints) {
+ my %f; $f{$_}++ for (@ints);
+ 0 + (keys(%f) == uniq values(%f));
+}
+
+is(uniqueOccurences(1,2,2,1,1,3),1);
+is(uniqueOccurences(1,2,3),0);
+is(uniqueOccurences(-2,0,1,-2,1,1,0,1,-2,9),1);
+
+done_testing;
diff --git a/challenge-260/perlboy1967/perl/ch2.pl b/challenge-260/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..fdb243e2a7
--- /dev/null
+++ b/challenge-260/perlboy1967/perl/ch2.pl
@@ -0,0 +1,37 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 260
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-260
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Dictionary Rank
+Submitted by: Mark Anderson
+
+You are given a word, $word.
+
+Write a script to compute the dictionary rank of the given word.
+(out of n combinations using its individual characters)
+
+=cut
+
+use v5.32;
+use feature q(signatures);
+use common::sense;
+
+use Test2::V0;
+
+use Algorithm::Combinatorics qw(permutations);
+use List::AllUtils qw(uniq firstidx);
+
+sub dictionaryRank ($word) {
+ 1 + firstidx {$_ eq $word} uniq sort map {join '',@$_} permutations([split //,$word]);
+}
+
+is(dictionaryRank('CAT'),3,'CAT');
+is(dictionaryRank('GOOGLE'),88,'GOOGLE');
+is(dictionaryRank('SECRET'),255,'SECRET');
+
+done_testing;