diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-03-11 13:31:39 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-03-11 13:31:39 -0400 |
| commit | 7bd27ed82b90bd4e0a91f038bf9c369601d423e8 (patch) | |
| tree | 3775251d0b24e4796c81abb993c6d4f754ce3379 | |
| parent | 77245595e193396bdb9347f5b5de7a088aea8bae (diff) | |
| download | perlweeklychallenge-club-7bd27ed82b90bd4e0a91f038bf9c369601d423e8.tar.gz perlweeklychallenge-club-7bd27ed82b90bd4e0a91f038bf9c369601d423e8.tar.bz2 perlweeklychallenge-club-7bd27ed82b90bd4e0a91f038bf9c369601d423e8.zip | |
DAJ 260
| -rw-r--r-- | challenge-260/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-260/dave-jacoby/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-260/dave-jacoby/perl/ch-2.pl | 35 |
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; +} |
