aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-02 16:50:45 +0100
committerGitHub <noreply@github.com>2024-04-02 16:50:45 +0100
commit1af8cd87c9f68460eaa0c3d73ab0713ae8434591 (patch)
tree6ac9c2839c1a95c6a88bc90636cc3c1deeb1e23b
parent597786308ff8f7d4fbf493789f7504e56db3e180 (diff)
parent2406b0be3877544fad2097bc0b37a5be321e69d1 (diff)
downloadperlweeklychallenge-club-1af8cd87c9f68460eaa0c3d73ab0713ae8434591.tar.gz
perlweeklychallenge-club-1af8cd87c9f68460eaa0c3d73ab0713ae8434591.tar.bz2
perlweeklychallenge-club-1af8cd87c9f68460eaa0c3d73ab0713ae8434591.zip
Merge pull request #9859 from pjcs00/wk263
Week 263
-rw-r--r--challenge-263/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-263/peter-campbell-smith/perl/ch-1.pl42
-rwxr-xr-xchallenge-263/peter-campbell-smith/perl/ch-2.pl41
3 files changed, 84 insertions, 0 deletions
diff --git a/challenge-263/peter-campbell-smith/blog.txt b/challenge-263/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..c5f9671906
--- /dev/null
+++ b/challenge-263/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/263
diff --git a/challenge-263/peter-campbell-smith/perl/ch-1.pl b/challenge-263/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..0e3ffcce74
--- /dev/null
+++ b/challenge-263/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-04-01
+use utf8; # Week 263 - task 1 - Target index
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+my (@ints, $j);
+
+target_index([1, 5, 3, 2, 4, 2], 2);
+target_index([1, 2, 4, 3, 5], 6);
+target_index([5, 3, 2, 4, 2, 1], 4);
+
+# bigger example
+for $j (0 .. 500) {
+ push @ints, int(rand(30) + 1);
+}
+target_index(\@ints, 15);
+
+sub target_index {
+
+ my (@ints, $int, $k, $m, $n, $output);
+
+ # initialise
+ @ints = @{$_[0]};
+ $k = $_[1];
+ say qq[\nInput: \@ints = (] . join(', ', @ints) . qq[), \$k = $k];
+
+ # count integers less than $k and equal to $k
+ $n = $m = 0;
+ for $int (@ints) {
+ $n ++ if $int < $k;
+ $m ++ if $int == $k;
+ }
+
+ # show results
+ $output = '';
+ $output .= qq[$_, ] for $n .. ($n + $m - 1);
+ say qq[Output: (] . substr($output, 0, -2) . ')';
+}
diff --git a/challenge-263/peter-campbell-smith/perl/ch-2.pl b/challenge-263/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..a1b303597d
--- /dev/null
+++ b/challenge-263/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-04-01
+use utf8; # Week 263 - task 2 - Merge items
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+my (@items1, @items2, $j);
+
+merge_items([[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]]);
+merge_items([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]]);
+merge_items([[1, 1], [2, 2], [3, 3] ], [[2,3], [2, 4]]);
+
+# larger example
+for $j (0 .. 30) {
+ push @items1, [int(rand(6)) + 1, int(rand(20)) + 1];
+ push @items2, [int(rand(6)) + 1, int(rand(20)) + 1];
+}
+
+merge_items(\@items1, \@items2);
+
+sub merge_items {
+
+ my (@items, $j, $item, %quantity, @input, $output);
+
+ # combine inventories
+ @items = @_;
+ for $j (0 .. 1) {
+ for $item (@{$items[$j]}) {
+ $quantity{$item->[0]} += $item->[1];
+ $input[$j] .= qq{[$item->[0], $item->[1]], };
+ }
+ }
+
+ # format output
+ $output .= qq{[$_, $quantity{$_}], } for (sort keys %quantity);
+ say qq[\nInput: \@items1 = ] . substr($input[0], 0, -2) . qq[, \@items2 = ] . substr($input[1], 0, -2);
+ say qq[Output: ] . substr($output, 0, -2);
+}