From 7bd27ed82b90bd4e0a91f038bf9c369601d423e8 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 11 Mar 2024 13:31:39 -0400 Subject: DAJ 260 --- challenge-260/dave-jacoby/blog.txt | 1 + challenge-260/dave-jacoby/perl/ch-1.pl | 37 ++++++++++++++++++++++++++++++++++ challenge-260/dave-jacoby/perl/ch-2.pl | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 challenge-260/dave-jacoby/blog.txt create mode 100644 challenge-260/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-260/dave-jacoby/perl/ch-2.pl 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; +} -- cgit