aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-06 22:33:16 +0100
committerGitHub <noreply@github.com>2024-05-06 22:33:16 +0100
commit24af0171a27c17dd7beb0a41fa1f6620962be33b (patch)
treefa659be9370f2c57c2f546e0df11b6663aaca066
parent097d75c029dedbe6d8c9c6aee881fa86bee7d788 (diff)
parent7b6dbcf59d867da8a01679113fa6d470095652b5 (diff)
downloadperlweeklychallenge-club-24af0171a27c17dd7beb0a41fa1f6620962be33b.tar.gz
perlweeklychallenge-club-24af0171a27c17dd7beb0a41fa1f6620962be33b.tar.bz2
perlweeklychallenge-club-24af0171a27c17dd7beb0a41fa1f6620962be33b.zip
Merge pull request #10053 from jacoby/master
DAJ 268
-rw-r--r--challenge-268/dave-jacoby/perl/ch-1.pl46
-rw-r--r--challenge-268/dave-jacoby/perl/ch-2.pl38
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-268/dave-jacoby/perl/ch-1.pl b/challenge-268/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..44ecb815d1
--- /dev/null
+++ b/challenge-268/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc postderef say signatures state };
+
+use List::Util qw{ uniq any };
+
+my @examples = (
+
+ [ [ 3, 7, 5 ], [ 9, 5, 7 ], ],
+ [ [ 1, 2, 1 ], [ 5, 4, 4 ], ],
+ [ [2], [5], ],
+);
+
+for my $example (@examples) {
+ my $output = find_magic_number(@$example);
+ my $x = join ',', $example->[0]->@*;
+ my $y = join ',', $example->[1]->@*;
+ say <<"END";
+ Input: \@x = ($x)
+ \@y = ($y)
+ Output: $output
+END
+}
+
+sub find_magic_number (@arrays) {
+ my ( $x, $y ) = @arrays;
+ my %hash;
+ my %o;
+ my @n;
+ for my $v ( $x->@* ) {
+ my @o = map { abs $v - $_ } $y->@*;
+ $hash{$v}->@* = @o;
+ push @n, @o;
+ }
+ for my $v ( uniq sort @n ) {
+ my $c = 0;
+ for my $k ( keys %hash ) {
+ $c++ if any { $v == $_ } $hash{$k}->@*;
+ }
+ $o{$v} = $c;
+ }
+ my @o = sort { $o{$b} <=> $o{$a} } keys %o;
+ return shift @o;
+}
diff --git a/challenge-268/dave-jacoby/perl/ch-2.pl b/challenge-268/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..f11acbc962
--- /dev/null
+++ b/challenge-268/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+use List::Util qw{ first };
+
+my @examples = (
+
+ [ 2, 5, 3, 4 ],
+ [ 9, 4, 1, 3, 6, 4, 6, 1 ],
+ [ 1, 2, 2, 3 ],
+);
+
+for my $example (@examples) {
+ my @output = number_game( $example->@* );
+ my $output = join ', ', @output;
+ my $ints = join ', ', $example->@*;
+
+ say <<"END";
+ Input: \@ints = ($ints)
+ Output: ($output)
+END
+}
+
+sub number_game (@ints) {
+ my @sorted = sort { $a <=> $b } @ints;
+ my @output;
+ while (@sorted) {
+ my @cache;
+ push @cache, shift @sorted;
+ push @cache, shift @sorted;
+ @cache = reverse @cache;
+ push @output, @cache;
+ }
+ return @output;
+}