diff options
| -rw-r--r-- | challenge-252/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-252/dave-jacoby/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-252/dave-jacoby/perl/ch-2.pl | 44 | ||||
| -rw-r--r-- | challenge-253/dave-jacoby/perl/ch-1.pl | 48 | ||||
| -rw-r--r-- | challenge-253/dave-jacoby/perl/ch-2.pl | 61 |
5 files changed, 196 insertions, 0 deletions
diff --git a/challenge-252/dave-jacoby/blog.txt b/challenge-252/dave-jacoby/blog.txt new file mode 100644 index 0000000000..042018a7eb --- /dev/null +++ b/challenge-252/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2024/01/15/camelia-goes-to-monte-carlo-weekly-challenge-252.html diff --git a/challenge-252/dave-jacoby/perl/ch-1.pl b/challenge-252/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..f1b874189f --- /dev/null +++ b/challenge-252/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + [ 1, 2, 3, 4 ], + [ 2, 7, 1, 19, 18, 3 ], +); + +for my $example (@examples) { + my $input = join ', ', $example->@*; + my $output = special_numbers( $example->@* ); + + say <<~"END"; + Input: \$ints = ($input) + Output: $output + END +} + +sub special_numbers (@input) { + my $output = 0; + my $n = scalar @input; + return + sum0 + map { $input[ $_ - 1 ] ** 2 } + grep { $n % $_ == 0 } + 1 .. scalar @input; + + ## the longer form I wrote first + # for my $i ( 1 .. scalar @input ) { + # if ( $n % $i == 0 ) { + # my $v = $input[ $i - 1 ]; + # $output += ( $v**2 ); + # } + # } + # return $output; +} diff --git a/challenge-252/dave-jacoby/perl/ch-2.pl b/challenge-252/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..18f840974b --- /dev/null +++ b/challenge-252/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; +use JSON; +my $j = JSON->new->pretty; + +my @examples = ( 1, 3, 4, 5, 7 ); +for my $input (@examples) { + my $o = unique_sum_zero($input); + my $output = join ', ', $o->@*; + + say <<~"END"; + Input: \$matrix = $input + Output: ($output) + END +} + +sub unique_sum_zero ( $input, $list = [] ) { + return [0] if $input == 1; # handle zero case + if ( $input == scalar $list->@* ) { # control recursion + return $list if 0 == sum0 $list->@*; + return -1; + } + my $c = 0; + while (1) { + my @list = $list->@*; + my $n = -9 + int rand 19; # generates a number between -9 and 9 + next if grep { $_ == $n } @list; # removes duplicates + push @list, $n; + my $return = unique_sum_zero( $input, \@list ); + if ( ref $return && ref $return eq 'ARRAY' ) { + return $return; + } + + # if you have bad numbers, it'll be hard to recover + # so we'll give up after a while + return -1 if $c++ > 100; + } + return -1; +} diff --git a/challenge-253/dave-jacoby/perl/ch-1.pl b/challenge-253/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..02d8c81875 --- /dev/null +++ b/challenge-253/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + { + words => [ "one.two.three", "four.five", "six" ], + separator => ".", + }, + { + words => [ '$perl$$', '$$raku$' ], + separator => '$', + }, + { + words => [qw{abracadabra}], + separator => 'a', + }, + { + words => [qw{Splits the string EXPR into a list of strings}], + separator => 'i', + } +); + +for my $example (@examples) { + my $input = join ', ', map { qq{"$_"} } $example->{words}->@*; + my @output = split_strings($example); + my $separator = $example->{separator}; + my $output = join ', ', map { qq{"$_"} } @output; + + say <<~"END"; + Input: \@words = ($input) + \$separator = "$separator" + Output: $output + + END +} + +sub split_strings ($hash) { + my $sep = quotemeta( $hash->{separator} ); + my @words = $hash->{words}->@*; + my @output = grep { length $_ } map { split /$sep/, $_ } @words; + return @output; +} diff --git a/challenge-253/dave-jacoby/perl/ch-2.pl b/challenge-253/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..0dd5e50090 --- /dev/null +++ b/challenge-253/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max sum0 }; + +my @examples = ( + + [ + [ 1, 1, 0, 0, 0 ], + [ 1, 1, 1, 1, 0 ], + [ 1, 0, 0, 0, 0 ], + [ 1, 1, 0, 0, 0 ], + [ 1, 1, 1, 1, 1 ] + ], + [ + [ 1, 0, 0, 0 ], + [ 1, 1, 1, 1 ], + [ 1, 0, 0, 0 ], + [ 1, 0, 0, 0 ] + ], +); + +for my $input (@examples) { + my @o = weakest_row($input); + my $output = join ', ', @o; + my $matrix = format_matrix($input); + + say <<~"END"; + Input: \$matrix = $matrix; + Output: ($output) + END +} + +sub weakest_row ($matrix) { + my %rank; + for my $i ( 0 .. -1 + scalar $matrix->@* ) { + my $r = $matrix->[$i]; + my $s = sum0 $r->@*; + $rank{$i} = $s; + } + my @output = sort { $rank{$a} <=> $rank{$b} } sort keys %rank; + return @output; +} + +sub format_matrix ($matrix) { + my $maxlen = max map { length $_ } map { $_->@* } $matrix->@*; + my $output = join "\n ", '[', ( + map { qq{ [$_],} } map { + join ',', + map { pad( $_, 1 + $maxlen ) } + $_->@* + } map { $matrix->[$_] } 0 .. -1 + scalar $matrix->@* + ), + ']'; + return $output; +} + +sub pad ( $str, $len = 4 ) { return sprintf "%${len}s", $str; } |
