aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-03 11:35:26 +0100
committerGitHub <noreply@github.com>2024-04-03 11:35:26 +0100
commit98b7d7b7287a922c6c79d272dcfe72774ddbbfb2 (patch)
tree34e1f372fae39e0ef66ce24df8fa4b9c4933ceee
parentf1be0a7ea43632d0763a7506d164a61ccddc6b06 (diff)
parentae28c878d0b2588ba22c34975e046543572acfb2 (diff)
downloadperlweeklychallenge-club-98b7d7b7287a922c6c79d272dcfe72774ddbbfb2.tar.gz
perlweeklychallenge-club-98b7d7b7287a922c6c79d272dcfe72774ddbbfb2.tar.bz2
perlweeklychallenge-club-98b7d7b7287a922c6c79d272dcfe72774ddbbfb2.zip
Merge pull request #9860 from jacoby/master
DAJ 263
-rw-r--r--challenge-263/dave-jacoby/blog.txt1
-rw-r--r--challenge-263/dave-jacoby/perl/ch-1.pl30
-rw-r--r--challenge-263/dave-jacoby/perl/ch-2.pl48
-rw-r--r--challenge-263/dave-jacoby/python/ch-1.py24
4 files changed, 103 insertions, 0 deletions
diff --git a/challenge-263/dave-jacoby/blog.txt b/challenge-263/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..638ccfed3a
--- /dev/null
+++ b/challenge-263/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby-lpwk.onrender.com/2024/04/02/arrayed-against-me-weekly-challenge-263.html
diff --git a/challenge-263/dave-jacoby/perl/ch-1.pl b/challenge-263/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..304bb4774b
--- /dev/null
+++ b/challenge-263/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @examples = (
+
+ { ints => [ 1, 5, 3, 2, 4, 2 ], k => 2 },
+ { ints => [ 1, 2, 4, 3, 5 ], k => 6 },
+ { ints => [ 5, 3, 2, 4, 2, 1 ], k => 4 },
+);
+
+for my $example (@examples) {
+ my @output = target_index($example);
+ my $output = join ', ', @output;
+ my $ints = join ', ', $example->{ints}->@*;
+ my $k = $example->{k};
+ say <<"END";
+ Input: \@ints = ($ints), \$k = $k
+ Output: ($output)
+END
+}
+
+sub target_index ($obj) {
+ my @sorted = sort { $a <=> $b } $obj->{ints}->@*;
+ my $k = $obj->{k};
+ my @output = grep { $k == $sorted[$_] } 0 .. $#sorted;
+ return @output;
+}
diff --git a/challenge-263/dave-jacoby/perl/ch-2.pl b/challenge-263/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..32b7651278
--- /dev/null
+++ b/challenge-263/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @examples = (
+
+ {
+ items1 => [ [ 1, 1 ], [ 2, 1 ], [ 3, 2 ] ],
+ items2 => [ [ 2, 2 ], [ 1, 3 ] ],
+ },
+ {
+ items1 => [ [ 1, 2 ], [ 2, 3 ], [ 1, 3 ], [ 3, 2 ] ],
+ items2 => [ [ 3, 1 ], [ 1, 3 ] ],
+ },
+ {
+ items1 => [ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ] ],
+ items2 => [ [ 2, 3 ], [ 2, 4 ] ],
+ }
+);
+
+for my $example (@examples) {
+ my @output = merge_items($example);
+ my $output = join ', ', map { make_block($_) } @output;
+ my $items1 = join ', ', map { make_block($_) } $example->{items1}->@*;
+ my $items2 = join ', ', map { make_block($_) } $example->{items2}->@*;
+
+ say <<"END";
+ Input: \@items1 = [ $items1 ]
+ \@items2 = [ $items2 ]
+ Output: [ $output ]
+END
+}
+
+sub make_block ($ref) {
+ my $list = join ',', $ref->@*;
+ return qq{[$list]};
+}
+
+sub merge_items ($example) {
+ my %output;
+ for my $p ( $example->{items1}->@*, $example->{items2}->@* ) {
+ my ( $item_id, $quantity ) = $p->@*;
+ $output{$item_id} += $quantity;
+ }
+ return map { [ int $_, $output{$_} ] } sort keys %output;
+}
diff --git a/challenge-263/dave-jacoby/python/ch-1.py b/challenge-263/dave-jacoby/python/ch-1.py
new file mode 100644
index 0000000000..7490ef961c
--- /dev/null
+++ b/challenge-263/dave-jacoby/python/ch-1.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+
+def main():
+ examples = [ { 'k':2, 'ints':[1, 5, 3, 2, 4, 2 ] }, { 'k':6, 'ints':[1, 2, 4, 3, 5 ] }, { 'k':4, 'ints':[5, 3, 2, 4, 2, 1 ] } ]
+ for e in examples:
+ output = target_index( e )
+ o = ",".join(output)
+ ints = e["ints"].copy()
+ i = ','.join(map(str,ints))
+ k = str(e["k"])
+ print("Input: k={}, i=[{}]\nOutput: output=[{}]\n".format(k,i,o))
+
+def target_index( obj ):
+ output = []
+ k = obj["k"]
+ ints = obj["ints"].copy()
+ ints.sort()
+ for i, item in enumerate(ints):
+ if item == k:
+ output.append(str(i))
+ return(output)
+
+if __name__ == '__main__':
+ main()