diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-16 00:30:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-16 00:30:36 +0100 |
| commit | 2685e5b310744d7916e68448d6a91a56787ca15f (patch) | |
| tree | b72a2fb7332fb4b530fd0a1a340d833140241dbf | |
| parent | 3cb6e884c2eab640656b69166f1deace2dbb55c9 (diff) | |
| parent | a31fc60717848459d5c7536ae2bb273c234ab270 (diff) | |
| download | perlweeklychallenge-club-2685e5b310744d7916e68448d6a91a56787ca15f.tar.gz perlweeklychallenge-club-2685e5b310744d7916e68448d6a91a56787ca15f.tar.bz2 perlweeklychallenge-club-2685e5b310744d7916e68448d6a91a56787ca15f.zip | |
Merge pull request #12029 from jacoby/master
DAJ 321, plus last week's forgotten commit
| -rw-r--r-- | challenge-320/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-320/dave-jacoby/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-320/dave-jacoby/perl/ch-2.pl | 30 | ||||
| -rw-r--r-- | challenge-321/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-321/dave-jacoby/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-321/dave-jacoby/perl/ch-2.pl | 47 |
6 files changed, 146 insertions, 0 deletions
diff --git a/challenge-320/dave-jacoby/blog.txt b/challenge-320/dave-jacoby/blog.txt new file mode 100644 index 0000000000..128f71d346 --- /dev/null +++ b/challenge-320/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/05/08/lots-and-lots-of-integers-weekly-challenge-320.html diff --git a/challenge-320/dave-jacoby/perl/ch-1.pl b/challenge-320/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..052d35c437 --- /dev/null +++ b/challenge-320/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + [ -3, -2, -1, 1, 2, 3 ], + [ -2, -1, 0, 0, 1 ], + [ 1, 2, 3, 4 ], +); + +for my $example (@examples) { + my $str = join ', ', $example->@*; + my $output = max_count( $example->@* ); + say <<"END"; + Input: \$str = ($str) + Output: $output +END +} + +sub max_count(@ints) { + my $pos = scalar grep { $_ > 0 } @ints; + my $neg = scalar grep { $_ < 0 } @ints; + return $pos > $neg ? $pos : $neg; +} diff --git a/challenge-320/dave-jacoby/perl/ch-2.pl b/challenge-320/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..f5816533d5 --- /dev/null +++ b/challenge-320/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ 1, 23, 4, 5 ], + [ 1, 2, 3, 4, 5 ], + [ 1, 2, 34 ], +); + +for my $example (@examples) { + my $str = join ', ', $example->@*; + my $output = sum_diff( $example->@* ); + say <<"END"; + Input: \$str = ($str) + Output: $output +END +} + +sub sum_diff (@ints) { + my $digit_sum = sum0 @ints; + my $element_sum = sum0 map { split //, $_ } @ints; + return abs $digit_sum - $element_sum; +} + diff --git a/challenge-321/dave-jacoby/blog.txt b/challenge-321/dave-jacoby/blog.txt new file mode 100644 index 0000000000..e379140866 --- /dev/null +++ b/challenge-321/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/05/15/decreasing-order-oddly-weekly-challenge-321.html diff --git a/challenge-321/dave-jacoby/perl/ch-1.pl b/challenge-321/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..7248522cd8 --- /dev/null +++ b/challenge-321/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + [ 1, 2, 4, 3, 5, 6 ], + [ 0, 2, 4, 8, 3, 5 ], + [ 7, 3, 1, 0, 5, 9 ], +); + +for my $example (@examples) { + my $str = join ', ', $example->@*; + my $output = distinct_average( $example->@* ); + say <<"END"; + Input: \$str = ($str) + Output: $output +END +} + +sub distinct_average(@ints) { + # we're given an unsorted array but sorted would be more useful + # and we lose nothing + @ints = sort { $a <=> $b } @ints; + my %output; + # we remove the highest and lowest (first and last) + # elements from a list, average them, and add the values + # to a hash + while ( @ints ) { + my $min = shift @ints; + my $max = pop @ints; + my $avg = ( $min + $max ) / 2; + $output{$avg}++; + } + # we don't care about how many times each get used, + # merely the count + return scalar keys %output; +} diff --git a/challenge-321/dave-jacoby/perl/ch-2.pl b/challenge-321/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..e6425f2fb8 --- /dev/null +++ b/challenge-321/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + { + str1 => "ab#c", + str2 => "ad#c" + }, + { + str1 => "ab##", + str2 => "a#b#" + }, + { + str1 => "a#b", + str2 => "c" + }, +); + +for my $example (@examples) { + my $output = backspace_compare( $example ); + say <<"END"; + Input: \$str1 = "$example->{str1}" + \$str2 = "$example->{str2}" + Output: $output +END +} + +sub backspace_compare ($obj) { + my $str1 = $obj->{str1}; + my $str2 = $obj->{str2}; + my $back1 = remove_backspaces($str1); + my $back2 = remove_backspaces($str2); + return $back1 eq $back2 ? 'true' : 'false'; +} + +sub remove_backspaces ( $str ) { + while ( $str =~ /\w\#/mx ){ + $str =~ s/(\w\#)//mx; + } + return $str; +}
\ No newline at end of file |
