diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-03 21:59:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-03 21:59:30 +0100 |
| commit | 1fe16e5294fc3712e8ca2bb22f897ab893b64fa8 (patch) | |
| tree | eee5289849c7623499eee29dd0e8ab46d24abc9f | |
| parent | dabf254dc1122783dcbf5087e89aa5e436017fdb (diff) | |
| parent | cdcee49423969cd96e0905eaa7ba5701694f7a26 (diff) | |
| download | perlweeklychallenge-club-1fe16e5294fc3712e8ca2bb22f897ab893b64fa8.tar.gz perlweeklychallenge-club-1fe16e5294fc3712e8ca2bb22f897ab893b64fa8.tar.bz2 perlweeklychallenge-club-1fe16e5294fc3712e8ca2bb22f897ab893b64fa8.zip | |
Merge pull request #12128 from pme/challenge-324
challenge-324
| -rwxr-xr-x | challenge-324/peter-meszaros/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-324/peter-meszaros/perl/ch-2.pl | 78 | ||||
| -rwxr-xr-x | challenge-324/peter-meszaros/tcl/ch-1.tcl | 59 | ||||
| -rwxr-xr-x | challenge-324/peter-meszaros/tcl/ch-2.tcl | 82 |
4 files changed, 280 insertions, 0 deletions
diff --git a/challenge-324/peter-meszaros/perl/ch-1.pl b/challenge-324/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..823f1a6dcb --- /dev/null +++ b/challenge-324/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# +=head1 Task 1: 2D Array + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers and two integers $r amd $c. + +Write a script to create two dimension array having $r rows and $c columns +using the given array. + +=head2 Example 1 + + Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2 + Output: ([1, 2], [3, 4]) + +=head2 Example 2 + + Input: @ints = (1, 2, 3), $r = 1, $c = 3 + Output: ([1, 2, 3]) + +=head2 Example 3 + + Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1 + Output: ([1], [2], [3], [4]) + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[1, 2, 3, 4], 2, 2], [[1, 2], [3, 4]], "Example 1"], + [[[1, 2, 3], 1, 3], [[1, 2, 3]], "Example 2"], + [[[1, 2, 3, 4], 4, 1], [[1], [2], [3], [4]], "Example 3"], +]; + +sub two_d_array +{ + my $ints = $_[0]->[0]; + my $r = $_[0]->[1]; + my $c = $_[0]->[2]; + + return undef unless @$ints == ($r * $c); + + my @result; + for my $i (0 .. $r - 1) { + my @row = $ints->@[($i * $c) .. ($i * $c + $c - 1)]; + push @result, \@row; + } + return \@result; +} + +for (@$cases) { + is(two_d_array($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-324/peter-meszaros/perl/ch-2.pl b/challenge-324/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..3c4b0ac955 --- /dev/null +++ b/challenge-324/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Total XOR + +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return the sum of total XOR for every subset of given +array. + +=head2 Example 1 + + Input: @ints = (1, 3) + Output: 6 + + Subset [1], total XOR = 1 + Subset [3], total XOR = 3 + Subset [1, 3], total XOR => 1 XOR 3 => 2 + + Sum of total XOR => 1 + 3 + 2 => 6 + +=head2 Example 2 + + Input: @ints = (5, 1, 6) + Output: 28 + + Subset [5], total XOR = 5 + Subset [1], total XOR = 1 + Subset [6], total XOR = 6 + Subset [5, 1], total XOR => 5 XOR 1 => 4 + Subset [5, 6], total XOR => 5 XOR 6 => 3 + Subset [1, 6], total XOR => 1 XOR 6 => 7 + Subset [5, 1, 6], total XOR => 5 XOR 1 XOR 6 => 2 + + Sum of total XOR => 5 + 1 + 6 + 4 + 3 + 7 + 2 => 28 + +=head2 Example 3 + + Input: @ints = (3, 4, 5, 6, 7, 8) + Output: 480 + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use Algorithm::Combinatorics qw/combinations/; + +my $cases = [ + [[1, 3], 6, "Example 1"], + [[5, 1, 6], 28, "Example 2"], + [[3, 4, 5, 6, 7, 8], 480, "Example 3"], +]; + +sub total_xor +{ + my $ints = shift; + + my $result = 0; + for my $i (1 .. @$ints) { + my $iter = combinations($ints, $i); + while (my $c = $iter->next) { + my $sum = 0; + $sum ^= $_ for @$c; + $result += $sum; + } + } + return $result; +} + +for (@$cases) { + is(total_xor($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-324/peter-meszaros/tcl/ch-1.tcl b/challenge-324/peter-meszaros/tcl/ch-1.tcl new file mode 100755 index 0000000000..26976f63d5 --- /dev/null +++ b/challenge-324/peter-meszaros/tcl/ch-1.tcl @@ -0,0 +1,59 @@ +#!/usr/bin/env tclsh +# +# Task 1: 2D Array +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers and two integers $r amd $c. +# +# Write a script to create two dimension array having $r rows and $c columns +# using the given array. +# +# Example 1 +# +# Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2 +# Output: ([1, 2], [3, 4]) +# +# Example 2 +# +# Input: @ints = (1, 2, 3), $r = 1, $c = 3 +# Output: ([1, 2, 3]) +# +# Example 3 +# +# Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1 +# Output: ([1], [2], [3], [4]) +# + +package require tcltest + +set cases { + {{{1 2 3 4} 2 2} {{1 2} {3 4}} "Example 1"} + {{{1 2 3} 1 3} {{1 2 3}} "Example 2"} + {{{1 2 3 4} 4 1} {1 2 3 4} "Example 3"} +} + +proc two_d_array {p} { + set ints [lindex $p 0] + set r [lindex $p 1] + set c [lindex $p 2] + + if {[llength $ints] != $r * $c} { + return nil + } + + set result {} + for {set i 0} {$i < $r} {incr i} { + lappend result [lrange $ints [expr $i * $c] [expr ($i * $c) + $c - 1]] + } + return $result +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + two_d_array [lindex $case 0] + } [lindex $case 1] +} + +exit 0 diff --git a/challenge-324/peter-meszaros/tcl/ch-2.tcl b/challenge-324/peter-meszaros/tcl/ch-2.tcl new file mode 100755 index 0000000000..f056cf98db --- /dev/null +++ b/challenge-324/peter-meszaros/tcl/ch-2.tcl @@ -0,0 +1,82 @@ +#!/usr/bin/env tclsh +# +# Task 2: Total XOR +# +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers. +# +# Write a script to return the sum of total XOR for every subset of given +# array. +# +# Example 1 +# +# Input: @ints = (1, 3) +# Output: 6 +# +# Subset [1], total XOR = 1 +# Subset [3], total XOR = 3 +# Subset [1, 3], total XOR => 1 XOR 3 => 2 +# +# Sum of total XOR => 1 + 3 + 2 => 6 +# +# Example 2 +# +# Input: @ints = (5, 1, 6) +# Output: 28 +# +# Subset [5], total XOR = 5 +# Subset [1], total XOR = 1 +# Subset [6], total XOR = 6 +# Subset [5, 1], total XOR => 5 XOR 1 => 4 +# Subset [5, 6], total XOR => 5 XOR 6 => 3 +# Subset [1, 6], total XOR => 1 XOR 6 => 7 +# Subset [5, 1, 6], total XOR => 5 XOR 1 XOR 6 => 2 +# +# Sum of total XOR => 5 + 1 + 6 + 4 + 3 + 7 + 2 => 28 +# +# Example 3 +# +# Input: @ints = (3, 4, 5, 6, 7, 8) +# Output: 480 +# + +package require tcltest +package require math::combinatorics + +set cases { + {{1 3} 6 "Example 1"} + {{5 1 6} 28 "Example 2"} + {{3 4 5 6 7 8} 480 "Example 3"} +} + +proc total_xor {ints} { + + set len [llength $ints] + set result 0 + for {set i 1} {$i <= $len} {incr i} { + set comb [::math::combinatorics::combinationObj create "comb" $len $i] + $comb setElements $ints + set c [$comb nextElements] + while {[llength $c] == $i} { + set sum 0 + foreach elem $c { + set sum [expr $sum ^ $elem] + } + set result [expr $result + $sum] + set c [$comb nextElements] + } + $comb destroy + } + return $result +} + +tcltest::configure -verbose {pass} +foreach case $cases { + tcltest::test [lindex $case 2] {} { + total_xor [lindex $case 0] + } [lindex $case 1] +} + +exit 0 + |
