diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-07-31 10:36:20 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-07-31 10:36:20 -0400 |
| commit | 95b752b0db17acabcd8c3e8ebb1d828795fb51df (patch) | |
| tree | 4d3b9c007a52c6d6be4f46ab61e15247604c8d32 | |
| parent | ecd08452c0bc55f0072f8d260ec830d4ec8cccc9 (diff) | |
| download | perlweeklychallenge-club-95b752b0db17acabcd8c3e8ebb1d828795fb51df.tar.gz perlweeklychallenge-club-95b752b0db17acabcd8c3e8ebb1d828795fb51df.tar.bz2 perlweeklychallenge-club-95b752b0db17acabcd8c3e8ebb1d828795fb51df.zip | |
DAJ 228
| -rw-r--r-- | challenge-228/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-228/dave-jacoby/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-228/dave-jacoby/perl/ch-2.pl | 39 |
3 files changed, 72 insertions, 0 deletions
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; +} |
