aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-15 11:59:52 +0000
committerGitHub <noreply@github.com>2024-03-15 11:59:52 +0000
commitb9344e344c98ea3910f3aa3b40d98e428d381d9e (patch)
tree6c51332554df03656f3e86d1cf77c9ab92bff65e
parent227709518d98f05fca532a5b249e3ef739340c17 (diff)
parentf4718484b6268822dbb9bc8c5abdf2e86d78c0f1 (diff)
downloadperlweeklychallenge-club-b9344e344c98ea3910f3aa3b40d98e428d381d9e.tar.gz
perlweeklychallenge-club-b9344e344c98ea3910f3aa3b40d98e428d381d9e.tar.bz2
perlweeklychallenge-club-b9344e344c98ea3910f3aa3b40d98e428d381d9e.zip
Merge pull request #9740 from PerlBoy1967/branch-for-challenge-260
w260 - Task 1 & 2
-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;