aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-03-11 18:59:34 +0000
committerGitHub <noreply@github.com>2024-03-11 18:59:34 +0000
commita5faed18694c8397b7f42d27c85b4cf94f65c876 (patch)
tree6007d68bed2abd92be3f9b4c0a3dcd72e92caf0b
parentf1dece9b70ca341528ddf466851feb819b4b7180 (diff)
parent7bd27ed82b90bd4e0a91f038bf9c369601d423e8 (diff)
downloadperlweeklychallenge-club-a5faed18694c8397b7f42d27c85b4cf94f65c876.tar.gz
perlweeklychallenge-club-a5faed18694c8397b7f42d27c85b4cf94f65c876.tar.bz2
perlweeklychallenge-club-a5faed18694c8397b7f42d27c85b4cf94f65c876.zip
Merge pull request #9731 from jacoby/master
DAJ 260
-rw-r--r--challenge-260/dave-jacoby/blog.txt1
-rw-r--r--challenge-260/dave-jacoby/perl/ch-1.pl37
-rw-r--r--challenge-260/dave-jacoby/perl/ch-2.pl35
3 files changed, 73 insertions, 0 deletions
diff --git a/challenge-260/dave-jacoby/blog.txt b/challenge-260/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..b6a94a052f
--- /dev/null
+++ b/challenge-260/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby-lpwk.onrender.com/2024/03/11/13-x-20-weekly-challenge-260.html
diff --git a/challenge-260/dave-jacoby/perl/ch-1.pl b/challenge-260/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..82e4771895
--- /dev/null
+++ b/challenge-260/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use DateTime;
+use List::Util qw{ uniqint };
+
+my @examples = (
+
+ [ 1, 2, 2, 1, 1, 3 ],
+ [ 1, 2, 3 ],
+ [ -2, 0, 1, -2, 1, 1, 0, 1, -2, 9 ],
+);
+
+for my $example (@examples) {
+ my @ints = $example->@*;
+ my $ints = join ',', @ints;
+ my $output = unique_occurances(@ints);
+ say <<"END";
+ Input: \$ints = ($ints)
+ Output: $output
+END
+}
+
+sub unique_occurances (@ints) {
+ my %hash;
+ for my $i (@ints) {
+ $hash{$i}++;
+ }
+
+ # is there a more clever way to do this?
+ my $before = scalar values %hash;
+ my $after = uniqint values %hash;
+ return $before == $after ? 1 : 0;
+}
diff --git a/challenge-260/dave-jacoby/perl/ch-2.pl b/challenge-260/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..2d06d124aa
--- /dev/null
+++ b/challenge-260/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use Algorithm::Permute;
+use List::Util qw{ first uniq };
+
+my @examples = (qw{ CAT GOOGLE SECRET });
+
+for my $example (@examples) {
+ my $output = dictionary_rank($example);
+
+ say <<"END";
+ Input: \$word = '$example'
+ Output: $output
+END
+}
+
+sub dictionary_rank ($word) {
+ my @word = split //, $word;
+ my @list;
+ my $iter = Algorithm::Permute->new( \@word );
+ while ( my @p = $iter->next ) {
+ push @list, join '', @p;
+ }
+ @list = uniq sort @list;
+
+ # would normally worry about a not-there response, but
+ # since the permutations are based on the word, the word
+ # has to be in there.
+ my $i = first { $word eq $list[$_] } 0 .. scalar @list;
+ return $i + 1;
+}