aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-01-28 10:36:57 +0000
committerGitHub <noreply@github.com>2025-01-28 10:36:57 +0000
commitf3cc2f11227959de9daca2f27c96fb522f6941cb (patch)
tree70ae2e723e01409008e8619466771df561429955
parent5a0263af20d376d5bcee4cf78c4d4417d3a5bb8a (diff)
parent490c82143edb945767acb58444447a13e223c13a (diff)
downloadperlweeklychallenge-club-f3cc2f11227959de9daca2f27c96fb522f6941cb.tar.gz
perlweeklychallenge-club-f3cc2f11227959de9daca2f27c96fb522f6941cb.tar.bz2
perlweeklychallenge-club-f3cc2f11227959de9daca2f27c96fb522f6941cb.zip
Merge pull request #11503 from pme/challenge-306
challenge-306
-rwxr-xr-xchallenge-306/peter-meszaros/perl/ch-1.pl68
-rwxr-xr-xchallenge-306/peter-meszaros/perl/ch-2.pl69
-rwxr-xr-xchallenge-306/peter-meszaros/tcl/ch-1.tcl67
-rwxr-xr-xchallenge-306/peter-meszaros/tcl/ch-2.tcl69
4 files changed, 273 insertions, 0 deletions
diff --git a/challenge-306/peter-meszaros/perl/ch-1.pl b/challenge-306/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..8a2bb33e30
--- /dev/null
+++ b/challenge-306/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Odd Sum
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of positive integers, @ints.
+
+Write a script to return the sum of all possible odd-length subarrays of the
+given array. A subarray is a contiguous subsequence of the array.
+
+=head2 Example 1
+
+ Input: @ints = (2, 5, 3, 6, 4)
+ Output: 77
+
+ Odd length sub-arrays:
+ (2) => 2
+ (5) => 5
+ (3) => 3
+ (6) => 6
+ (4) => 4
+ (2, 5, 3) => 10
+ (5, 3, 6) => 14
+ (3, 6, 4) => 13
+ (2, 5, 3, 6, 4) => 20
+
+ Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77
+
+=head2 Example 2
+
+ Input: @ints = (1, 3)
+ Output: 4
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[2, 5, 3, 6, 4], 77, 'Example 1'],
+ [[1, 3], 4, 'Example 2'],
+];
+
+sub odd_sum
+{
+ my $l = shift;
+
+ my $sum = 0;
+ for my $i (0 .. $#$l) {
+ for my $j ($i .. $#$l) {
+ my $len = $j - $i + 1;
+ if ($len % 2) {
+ $sum += $l->[$_] for $i .. $j;
+ }
+ }
+ }
+ return $sum;
+}
+
+for (@$cases) {
+ is(odd_sum($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-306/peter-meszaros/perl/ch-2.pl b/challenge-306/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..a74a55737a
--- /dev/null
+++ b/challenge-306/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,69 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Last Element
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given a array of integers, @ints.
+
+Write a script to play a game where you pick two biggest integers in the given
+array, say x and y. Then do the following:
+
+ a) if x == y then remove both from the given array
+ b) if x != y then remove x and replace y with (y - x)
+
+At the end of the game, there is at most one element left.
+
+Return the last element if found otherwise return 0.
+
+=head2 Example 1
+
+ Input: @ints = (3, 8, 5, 2, 9, 2)
+ Output: 1
+
+ Step 1: pick 8 and 9 => (3, 5, 2, 1, 2)
+ Step 2: pick 3 and 5 => (2, 2, 1, 2)
+ Step 3: pick 2 and 1 => (1, 2, 2)
+ Step 4: pick 2 and 1 => (1, 2)
+ Step 5: pick 1 and 2 => (1)
+
+=head2 Example 2
+
+ Input: @ints = (3, 2, 5)
+ Output: 0
+
+ Step 1: pick 3 and 5 => (2, 2)
+ Step 2: pick 2 and 2 => ()
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[3, 8, 5, 2, 9, 2], 1, 'Example 1'],
+ [[3, 2, 5], 0, 'Example 2'],
+];
+
+sub last_element
+{
+ my $l = shift;
+
+ while (@$l > 1) {
+ my @sorted = sort { $b <=> $a } @$l;
+ my $x = shift @sorted;
+ my $y = shift @sorted;
+ push @sorted, $x - $y if $x != $y;
+ $l = \@sorted;
+ }
+ return @$l ? $l->[0] : 0;
+}
+
+for (@$cases) {
+ is(last_element($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-306/peter-meszaros/tcl/ch-1.tcl b/challenge-306/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..b573e65a85
--- /dev/null
+++ b/challenge-306/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,67 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Odd Sum
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of positive integers, @ints.
+#
+# Write a script to return the sum of all possible odd-length subarrays of the
+# given array. A subarray is a contiguous subsequence of the array.
+#
+# Example 1
+#
+# Input: @ints = (2, 5, 3, 6, 4)
+# Output: 77
+#
+# Odd length sub-arrays:
+# (2) => 2
+# (5) => 5
+# (3) => 3
+# (6) => 6
+# (4) => 4
+# (2, 5, 3) => 10
+# (5, 3, 6) => 14
+# (3, 6, 4) => 13
+# (2, 5, 3, 6, 4) => 20
+#
+# Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77
+#
+# Example 2
+#
+# Input: @ints = (1, 3)
+# Output: 4
+#
+
+package require tcltest
+
+set cases {
+ {{2 5 3 6 4} 77 "Example 1"}
+ {{1 3} 4 "Example 2"}
+}
+
+proc odd_sum {p} {
+
+ set sum 0
+ for {set i 0} {$i < [llength $p]} {incr i} {
+ for {set j $i} {$j < [llength $p]} {incr j} {
+ set len [expr {$j - $i + 1}]
+ if {$len % 2} {
+ for {set k $i} {$k <= $j} {incr k} {
+ incr sum [lindex $p $k]
+ }
+ }
+ }
+ }
+ return $sum
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ odd_sum [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-306/peter-meszaros/tcl/ch-2.tcl b/challenge-306/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..d22a35101a
--- /dev/null
+++ b/challenge-306/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,69 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Last Element
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a array of integers, @ints.
+#
+# Write a script to play a game where you pick two biggest integers in the given
+# array, say x and y. Then do the following:
+#
+# a) if x == y then remove both from the given array
+# b) if x != y then remove x and replace y with (y - x)
+#
+# At the end of the game, there is at most one element left.
+#
+# Return the last element if found otherwise return 0.
+#
+# Example 1
+#
+# Input: @ints = (3, 8, 5, 2, 9, 2)
+# Output: 1
+#
+# Step 1: pick 8 and 9 => (3, 5, 2, 1, 2)
+# Step 2: pick 3 and 5 => (2, 2, 1, 2)
+# Step 3: pick 2 and 1 => (1, 2, 2)
+# Step 4: pick 2 and 1 => (1, 2)
+# Step 5: pick 1 and 2 => (1)
+#
+# Example 2
+#
+# Input: @ints = (3, 2, 5)
+# Output: 0
+#
+# Step 1: pick 3 and 5 => (2, 2)
+# Step 2: pick 2 and 2 => ()
+#
+
+package require tcltest
+
+set cases {
+ {{3 8 5 2 9 2} 1 "Example 1"}
+ {{3 2 5} 0 "Example 2"}
+}
+
+proc last_element {p} {
+ while {[llength $p] > 1} {
+ set sorted [lsort -integer -decreasing $p]
+ set x [lindex $sorted 0]
+ set y [lindex $sorted 1]
+ set sorted [lreplace $sorted 0 1]
+
+ if {$x != $y} {
+ lappend sorted [expr $x - $y]
+ }
+ set p $sorted
+ }
+
+ return [expr {[llength $p] ? [lindex $p 0] : 0}]
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ last_element [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0