aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2025-05-22 20:18:13 -0400
committerDave Jacoby <jacoby.david@gmail.com>2025-05-22 20:18:13 -0400
commite112ffc0c04b37f5b1f074d6b699eb771c80af8e (patch)
tree0a0b1ea029122cc6f92fe951e331fec904fb848c
parentd9a8a0d7aba084dc687dd97473f8739db9387b5c (diff)
downloadperlweeklychallenge-club-e112ffc0c04b37f5b1f074d6b699eb771c80af8e.tar.gz
perlweeklychallenge-club-e112ffc0c04b37f5b1f074d6b699eb771c80af8e.tar.bz2
perlweeklychallenge-club-e112ffc0c04b37f5b1f074d6b699eb771c80af8e.zip
DAJ 322 blogged
-rw-r--r--challenge-322/dave-jacoby/blog.txt1
-rw-r--r--challenge-322/dave-jacoby/perl/ch-1.pl50
-rw-r--r--challenge-322/dave-jacoby/perl/ch-2.pl32
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-322/dave-jacoby/blog.txt b/challenge-322/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..5cc9bdcac7
--- /dev/null
+++ b/challenge-322/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby-lpwk.onrender.com/2025/05/22/dashing-this-off-weekly-challenge-332.html
diff --git a/challenge-322/dave-jacoby/perl/ch-1.pl b/challenge-322/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..9d9c2cf31f
--- /dev/null
+++ b/challenge-322/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+
+use List::Util qw{ sum0 };
+
+my @examples = (
+
+ {
+ str => 'ABC-D-E-F',
+ i => 3,
+ },
+ {
+ str => 'A-BC-D-E',
+ i => 2,
+ },
+ {
+ str => '-A-BC-DE',
+ i => 4,
+ },
+);
+
+for my $example (@examples) {
+ my $output = string_format($example);
+ say <<"END";
+ Input: \$str = "$example->{str}"
+ \$i = $example->{i}
+ Output: $output
+END
+}
+
+sub string_format ($obj) {
+ my $i = $obj->{i};
+ my $str = $obj->{str};
+ $str = substr( $str, 1 ) while $str =~ /^-/mx;
+ my $l = length $str;
+ for my $x ( reverse 0 .. $l ) {
+ my $c = substr( $str, $x, 1 );
+ next if $c =~ /\w/mx;
+ next if $x + 1 >= $l;
+ my $group = substr( $str, $x + 1 );
+ ($group) = split /-/, $group;
+ if ( length $group < $i ) {
+ substr( $str, $x, 1 ) = '';
+ }
+ }
+ return $str;
+}
diff --git a/challenge-322/dave-jacoby/perl/ch-2.pl b/challenge-322/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..1e97662c23
--- /dev/null
+++ b/challenge-322/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+use List::Util qw{ uniq };
+
+my @examples = (
+
+ [ 55, 22, 44, 33 ],
+ [ 10, 10, 10 ],
+ [ 5, 1, 1, 4, 3 ],
+);
+
+for my $example (@examples) {
+ my @ints = $example->@*;
+ my $ints = join ', ', @ints;
+ my @output = rank_array( $example->@* );
+ my $output = join ', ', @output;
+ say <<"END";
+ Input: \@ints = ($ints)
+ Output: ($output)
+END
+}
+
+sub rank_array (@ints) {
+ my @sorted = uniq sort { $a <=> $b } @ints;
+ my %ranks;
+ my $r = 1;
+ for my $s (@sorted) { $ranks{$s} = $r++; }
+ return map { $ranks{$_} } @ints;
+}