aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-01-16 17:27:07 -0500
committerDave Jacoby <jacoby.david@gmail.com>2024-01-16 17:27:07 -0500
commit13ec6df65ba66ba1c002f8e2a4d8293c9f8f515f (patch)
tree3283e5e030a3b562c36be2bdfdf7d82caf53701f /challenge-252
parent7efb373bb9adffa79f84825217015835805298b5 (diff)
downloadperlweeklychallenge-club-13ec6df65ba66ba1c002f8e2a4d8293c9f8f515f.tar.gz
perlweeklychallenge-club-13ec6df65ba66ba1c002f8e2a4d8293c9f8f515f.tar.bz2
perlweeklychallenge-club-13ec6df65ba66ba1c002f8e2a4d8293c9f8f515f.zip
DAJ #252
Diffstat (limited to 'challenge-252')
-rw-r--r--challenge-252/dave-jacoby/blog.txt1
-rw-r--r--challenge-252/dave-jacoby/perl/ch-1.pl42
-rw-r--r--challenge-252/dave-jacoby/perl/ch-2.pl44
3 files changed, 87 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;
+}