aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-06-07 18:57:29 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2025-06-07 18:57:29 +0200
commit564cacdc7e6ead25c69baf7e2c66d5f2ba646364 (patch)
tree01a31ab0db3456784bf08d2b58d8964a87723d99
parentefa631ada4e63b89e590e48d14b17f69c26495be (diff)
downloadperlweeklychallenge-club-564cacdc7e6ead25c69baf7e2c66d5f2ba646364.tar.gz
perlweeklychallenge-club-564cacdc7e6ead25c69baf7e2c66d5f2ba646364.tar.bz2
perlweeklychallenge-club-564cacdc7e6ead25c69baf7e2c66d5f2ba646364.zip
Add solution 324.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-324/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-324/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-324/jeanluc2020/perl/ch-1.pl71
-rwxr-xr-xchallenge-324/jeanluc2020/perl/ch-2.pl84
4 files changed, 157 insertions, 0 deletions
diff --git a/challenge-324/jeanluc2020/blog-1.txt b/challenge-324/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..158cbc945a
--- /dev/null
+++ b/challenge-324/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-324-1.html
diff --git a/challenge-324/jeanluc2020/blog-2.txt b/challenge-324/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..aefac2c753
--- /dev/null
+++ b/challenge-324/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-324-2.html
diff --git a/challenge-324/jeanluc2020/perl/ch-1.pl b/challenge-324/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..f4b111a8f9
--- /dev/null
+++ b/challenge-324/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-324/#TASK1
+#
+# Task 1: 2D Array
+# ================
+#
+# 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])
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just loop over the amount of rows and over the amount
+# of columns, constructing the rows by creating the elements
+# for it, and then in the end we have our result.
+# If there are not enough elements in the input array, we return
+# right away. If there are more elements in the array than are
+# required, we just skip the remaining elements in the input
+# array.
+
+use v5.36;
+
+twod_array( [1, 2, 3, 4], 2, 2 );
+twod_array( [1, 2, 3], 1, 3 );
+twod_array( [1, 2, 3, 4], 4, 1 );
+twod_array( [1, 2, 3], 2, 2 );
+twod_array( [1, 2, 3, 4, 5], 2, 2 );
+
+sub twod_array( $ints, $r, $c ) {
+ say "Input: [" . join(", ", @$ints) . "], $r, $c";
+ my @result = ();
+ if(scalar(@$ints) < $r * $c) {
+ return say "Not enough elements in array!";
+ }
+ foreach my $i (0..$r-1) {
+ my @tmp = ();
+ foreach my $j (0..$c-1) {
+ push @tmp, shift @$ints;
+ }
+ push @result, [ @tmp ];
+ }
+
+ print "Output: [";
+ foreach my $elem (@result) {
+ print "[" . join(", ", @$elem) . "], ";
+ }
+ say "]";
+}
+
diff --git a/challenge-324/jeanluc2020/perl/ch-2.pl b/challenge-324/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..6864cd1d79
--- /dev/null
+++ b/challenge-324/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,84 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-324/#TASK2
+#
+# Task 2: Total XOR
+# =================
+#
+# 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
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we make sure that we have a set of unique elements,
+# eliminating duplicates using List::Util's uniq().
+# Then we create all possible subsets using Algorithm::Combinatorics'
+# subsets() - we use the iterator interface so we don't eat
+# unnecessary memory.
+# For each subset, we then calculate the xor of this subset. For
+# that, we start with 0, then we xor each element of the array to
+# the current value until there are no more elements. Then we add
+# that result to our current total value.
+#
+
+use v5.36;
+use List::Util qw( uniq );
+use Algorithm::Combinatorics qw( subsets );
+
+total_xor(1, 3);
+total_xor(5, 1, 6);
+total_xor(3, 4, 5, 6, 7, 8);
+
+sub total_xor( @ints ) {
+ say "Input: (" . join(", ", @ints) . ")";
+ my @set = uniq @ints;
+ my $iter = subsets( \@ints );
+ my $result = 0;
+ while ( my $set = $iter->next ) {
+ my $tmp = 0;
+ foreach my $elem (@$set) {
+ $tmp = $tmp ^ $elem;
+ }
+ $result += $tmp;
+ }
+ say "Output: $result";
+}
+