aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-22 23:37:53 +0100
committerGitHub <noreply@github.com>2025-10-22 23:37:53 +0100
commita8064e5126551cea953725ec2c39c91a5354443b (patch)
tree63145a58c65374b5bdfd9c58902ae15f3f59ff03
parent0d3a5b1cedaa532b32c5eb45b1a1fbd216c396c0 (diff)
parent8b2dc32d5d87607164d572d576a1dc2d8f9e3a38 (diff)
downloadperlweeklychallenge-club-a8064e5126551cea953725ec2c39c91a5354443b.tar.gz
perlweeklychallenge-club-a8064e5126551cea953725ec2c39c91a5354443b.tar.bz2
perlweeklychallenge-club-a8064e5126551cea953725ec2c39c91a5354443b.zip
Merge pull request #12900 from pme/challenge-344
challenge-344
-rwxr-xr-xchallenge-344/peter-meszaros/perl/ch-1.pl67
-rwxr-xr-xchallenge-344/peter-meszaros/perl/ch-2.pl107
-rwxr-xr-xchallenge-344/peter-meszaros/tcl/ch-1.tcl65
-rwxr-xr-xchallenge-344/peter-meszaros/tcl/ch-2.tcl105
4 files changed, 344 insertions, 0 deletions
diff --git a/challenge-344/peter-meszaros/perl/ch-1.pl b/challenge-344/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..b433f399f5
--- /dev/null
+++ b/challenge-344/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Array Form Compute
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given an array of integers, @ints and an integer, $x.
+
+Write a script to add $x to the integer in the array-form.
+
+ The array form of an integer is a digit-by-digit representation stored as
+ an array, where the most significant digit is at the 0th index.
+
+=head2 Example 1
+
+ Input: @ints = (1, 2, 3, 4), $x = 12
+ Output: (1, 2, 4, 6)
+
+=head2 Example 2
+
+ Input: @ints = (2, 7, 4), $x = 181
+ Output: (4, 5, 5)
+
+=head2 Example 3
+
+ Input: @ints = (9, 9, 9), $x = 1
+ Output: (1, 0, 0, 0)
+
+=head2 Example 4
+
+ Input: @ints = (1, 0, 0, 0, 0), $x = 9999
+ Output: (1, 9, 9, 9, 9)
+
+=head2 Example 5
+
+ Input: @ints = (0), $x = 1000
+ Output: (1, 0, 0, 0)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[[1, 2, 3, 4], 12], [1, 2, 4, 6], "Example 1"],
+ [[[2, 7, 4], 181], [4, 5, 5], "Example 2"],
+ [[[9, 9, 9], 1], [1, 0, 0, 0], "Example 3"],
+ [[[1, 0, 0, 0, 0], 9999], [1, 9, 9, 9, 9], "Example 4"],
+ [[[0], 1000], [1, 0, 0, 0], "Example 5"],
+];
+
+sub array_form_compute
+{
+ my $ints = $_[0]->[0];
+ my $x = $_[0]->[1];
+
+ return [split(//, join('', @$ints) + $x)];
+}
+
+for (@$cases) {
+ is(array_form_compute($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-344/peter-meszaros/perl/ch-2.pl b/challenge-344/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..392f6718d7
--- /dev/null
+++ b/challenge-344/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,107 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Array Formation
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given two list: @source and @target.
+
+Write a script to see if you can build the exact @target by putting these
+smaller lists from @source together in some order. You cannot break apart or
+change the order inside any of the smaller lists in @source.
+
+=head2 Example 1
+
+ Input: @source = ([2,3], [1], [4])
+ @target = (1, 2, 3, 4)
+ Output: true
+
+ Use in the order: [1], [2,3], [4]
+
+=head2 Example 2
+
+ Input: @source = ([1,3], [2,4])
+ @target = (1, 2, 3, 4)
+ Output: false
+
+=head2 Example 3
+
+ Input: @source = ([9,1], [5,8], [2])
+ @target = (5, 8, 2, 9, 1)
+ Output: true
+
+ Use in the order: [5,8], [2], [9,1]
+
+=head2 Example 4
+
+ Input: @source = ([1], [3])
+ @target = (1, 2, 3)
+ Output: false
+
+ Missing number: 2
+
+=head2 Example 5
+
+ Input: @source = ([7,4,6])
+ @target = (7, 4, 6)
+ Output: true
+
+ Use in the order: [7, 4, 6]
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+use constant { true => 1, false => 0 };
+
+my $cases = [
+ [[[[2,3], [1], [4]], [1, 2, 3, 4]], true, "Example 1"],
+ [[[[1,3], [2,4]], [1, 2, 3, 4]], false, "Example 2"],
+ [[[[9,1], [5,8], [2]], [5, 8, 2, 9, 1]], true, "Example 3"],
+ [[[[1], [3]], [1, 2, 3]], false, "Example 4"],
+ [[[[7,4,6]], [7, 4, 6]], true, "Example 5"],
+];
+
+sub array_formation
+{
+ my $source = $_[0]->[0];
+ my $target = $_[0]->[1];
+
+ sub _equal_arrays
+ {
+ my $source = shift;
+ my $target = shift;
+
+ return false if @$source != @$target;
+ for my $i (0 .. $#$source) {
+ return false if $source->[$i] != $target->[$i];
+ }
+ return true;
+ }
+
+ for my $i (0 .. $#$source) {
+ my $subarray = $source->[$i];
+ my $len = @$subarray;
+
+ my $found = false;
+ for my $j (0 .. $#$target - $len + 1) {
+ my $target_slice = [ $target->@[$j .. $j + $len - 1] ];
+ if (_equal_arrays($subarray, $target_slice)) {
+ splice(@$target, $j, $len);
+ $found = true;
+ last;
+ }
+ }
+ return false unless $found;
+ }
+ return @$target == 0 ? true : false;
+}
+
+for (@$cases) {
+ is(array_formation($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-344/peter-meszaros/tcl/ch-1.tcl b/challenge-344/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..2b54980bef
--- /dev/null
+++ b/challenge-344/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,65 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Array Form Compute
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints and an integer, $x.
+#
+# Write a script to add $x to the integer in the array-form.
+#
+# The array form of an integer is a digit-by-digit representation stored as
+# an array, where the most significant digit is at the 0th index.
+#
+# Example 1
+#
+# Input: @ints = (1, 2, 3, 4), $x = 12
+# Output: (1, 2, 4, 6)
+#
+# Example 2
+#
+# Input: @ints = (2, 7, 4), $x = 181
+# Output: (4, 5, 5)
+#
+# Example 3
+#
+# Input: @ints = (9, 9, 9), $x = 1
+# Output: (1, 0, 0, 0)
+#
+# Example 4
+#
+# Input: @ints = (1, 0, 0, 0, 0), $x = 9999
+# Output: (1, 9, 9, 9, 9)
+#
+# Example 5
+#
+# Input: @ints = (0), $x = 1000
+# Output: (1, 0, 0, 0)
+#
+
+package require tcltest
+
+set cases {
+ {{{1 2 3 4} 12} {1 2 4 6} "Example 1"}
+ {{{2 7 4} 181} {4 5 5} "Example 2"}
+ {{{9 9 9} 1} {1 0 0 0} "Example 3"}
+ {{{1 0 0 0 0} 9999} {1 9 9 9 9} "Example 4"}
+ {{{0} 1000} {1 0 0 0} "Example 5"}
+}
+
+proc array_form_compute {p} {
+ set ints [lindex $p 0]
+ set x [lindex $p 1]
+
+ return [split [expr [join $ints ""] + $x] ""]
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ array_form_compute [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-344/peter-meszaros/tcl/ch-2.tcl b/challenge-344/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..5252662c47
--- /dev/null
+++ b/challenge-344/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,105 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Array Formation
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given two list: @source and @target.
+#
+# Write a script to see if you can build the exact @target by putting these
+# smaller lists from @source together in some order. You cannot break apart or
+# change the order inside any of the smaller lists in @source.
+#
+# Example 1
+#
+# Input: @source = ([2,3], [1], [4])
+# @target = (1, 2, 3, 4)
+# Output: true
+#
+# Use in the order: [1], [2,3], [4]
+#
+# Example 2
+#
+# Input: @source = ([1,3], [2,4])
+# @target = (1, 2, 3, 4)
+# Output: false
+#
+# Example 3
+#
+# Input: @source = ([9,1], [5,8], [2])
+# @target = (5, 8, 2, 9, 1)
+# Output: true
+#
+# Use in the order: [5,8], [2], [9,1]
+#
+# Example 4
+#
+# Input: @source = ([1], [3])
+# @target = (1, 2, 3)
+# Output: false
+#
+# Missing number: 2
+#
+# Example 5
+#
+# Input: @source = ([7,4,6])
+# @target = (7, 4, 6)
+# Output: true
+#
+# Use in the order: [7, 4, 6]
+#
+
+package require tcltest
+
+set cases {
+ {{{{2 3} {1} {4}} {1 2 3 4}} true "Example 1"}
+ {{{{1 3} {2 4}} {1 2 3 4}} false "Example 2"}
+ {{{{9 1} {5 8} {2}} {5 8 2 9 1}} true "Example 3"}
+ {{{{1} {3}} {1 2 3}} false "Example 4"}
+ {{{{7 4 6}} {7 4 6}} true "Example 5"}
+}
+
+proc array_formation {p} {
+ set source [lindex $p 0]
+ set target [lindex $p 1]
+
+ proc _equal_arrays {source target} {
+ if {[llength $source] != [llength $target]} {
+ return false
+ }
+ for {set i 0} {$i < [llength $source]} {incr i} {
+ if {[lindex $source $i] != [lindex $target $i]} {
+ return false
+ }
+ }
+ return true
+ }
+
+ foreach subarray $source {
+ set len [llength $subarray]
+ set found 0
+ for {set j 0} {$j <= [expr [llength $target] - $len]} {incr j} {
+ set target_slice [lrange $target $j [expr $j + $len - 1]]
+ if {[_equal_arrays $subarray $target_slice]} {
+ set target [concat [lrange $target 0 [expr $j - 1]] \
+ [lrange $target [expr $j + $len] end]]
+ set found 1
+ break
+ }
+ }
+ if {!$found} {
+ return false
+ }
+ }
+ return [expr [llength $target] == 0 ? true : false]
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ array_formation [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+