From 7cccf2f51d1a1ad5027dcdf2fb9058cd7804808f Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 9 Oct 2023 17:41:11 -0400 Subject: DAJ 238 --- challenge-238/dave-jacoby/blog.txt | 1 + challenge-238/dave-jacoby/perl/ch-1.pl | 28 ++++++++++++++++++++++ challenge-238/dave-jacoby/perl/ch-2.pl | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-238/dave-jacoby/blog.txt create mode 100644 challenge-238/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-238/dave-jacoby/perl/ch-2.pl diff --git a/challenge-238/dave-jacoby/blog.txt b/challenge-238/dave-jacoby/blog.txt new file mode 100644 index 0000000000..3c53d53a96 --- /dev/null +++ b/challenge-238/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/10/09/you-cant.html diff --git a/challenge-238/dave-jacoby/perl/ch-1.pl b/challenge-238/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..fb07484a53 --- /dev/null +++ b/challenge-238/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ 1, 2, 3, 4, 5 ], + [ 1, 1, 1, 1, 1 ], + [ 0, -1, 1, 2 ], +); + +for my $e (@examples) { + my @output = running_sum( $e->@* ); + my $input = join ', ', $e->@*; + my $output = join ', ', @output; + say <<~"END"; + Input: \@int = ($input) + Output: ($output) + END +} + +sub running_sum (@int) { + my $c = 0; + my @output = map { $c += $_; $c } @int; + return @output; +} diff --git a/challenge-238/dave-jacoby/perl/ch-2.pl b/challenge-238/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..8b47668fa9 --- /dev/null +++ b/challenge-238/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Algorithm::Permute; +use List::Util qw{ product }; + +my @examples = ( + + [ 15, 99, 1, 34 ], + [ 50, 25, 33, 22 ], +); + +for my $e (@examples) { + my @int = $e->@*; + my $int = join ', ', @int; + my @output = persistence_sort(@int); + my $output = join ', ', @output; + say <<~"END"; + Input: \@int = ($int) + Output: ($output) + END +} + +sub persistence_sort (@nums) { + my @output = + map { $_->[0] } + sort { $a->[1] <=> $b->[1] } + map { [ $_, munge($_) ] } sort @nums; + return @output; +} + +sub munge ($i) { + my $c = 0; + while (1) { + return $c if $i < 10; + $i = product split //, $i; + $c++; + } + return -1; # just in case +} -- cgit