From 95b752b0db17acabcd8c3e8ebb1d828795fb51df Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 31 Jul 2023 10:36:20 -0400 Subject: DAJ 228 --- challenge-228/dave-jacoby/blog.txt | 1 + challenge-228/dave-jacoby/perl/ch-1.pl | 32 ++++++++++++++++++++++++++++ challenge-228/dave-jacoby/perl/ch-2.pl | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 challenge-228/dave-jacoby/blog.txt create mode 100644 challenge-228/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-228/dave-jacoby/perl/ch-2.pl diff --git a/challenge-228/dave-jacoby/blog.txt b/challenge-228/dave-jacoby/blog.txt new file mode 100644 index 0000000000..2b1a2630d8 --- /dev/null +++ b/challenge-228/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/07/31/get-sum-weekly-challenge-228.html diff --git a/challenge-228/dave-jacoby/perl/ch-1.pl b/challenge-228/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..4996d31312 --- /dev/null +++ b/challenge-228/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw( sum0 ); + +my @examples = ( + + [ 2, 1, 3, 2 ], + [ 1, 1, 1, 1 ], + [ 2, 1, 3, 4 ], +); + +for my $e (@examples) { + my @array = $e->@*; + my $array = join ', ', @array; + my $sum = uniq_sum(@array); + say <<~"END"; + Input: \@int = ($array) + Output: $sum + END +} + +sub uniq_sum (@array) { + my %hash; + for my $int (@array) { + $hash{$int}++; + } + return sum0 grep { $hash{$_} == 1 } keys %hash; +} diff --git a/challenge-228/dave-jacoby/perl/ch-2.pl b/challenge-228/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..512108a149 --- /dev/null +++ b/challenge-228/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw( min ); + +my @examples = ( + + [ 3, 4, 2 ], + [ 1, 2, 3 ], + +); + +for my $e (@examples) { + my @array = $e->@*; + my $array = join ', ', @array; + my $output = empty_array(@array); + say <<~"END"; + Input: \@int = ($array) + Output: $output + END +} + +# if the first element is the smallest +# then remove it +# else +# move it to the end +sub empty_array (@array) { + my $c = 0; + while ( scalar @array ) { + my $min = min @array; + my $next = shift @array; + if ( $min != $next ) { push @array, $next; } + $c++; + } + return $c; +} -- cgit