aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-08 22:50:10 +0100
committerGitHub <noreply@github.com>2025-06-08 22:50:10 +0100
commit73db1085b9c910bfa31f7c952c1ac8a76a06e09b (patch)
tree8f565af11fe059180b2dd89ddec074a001aceb24
parentd228b8e71acdd5e085ab6e60e8ba937c6965aea3 (diff)
parentf176e0edb6317066bbec46ccc940b313892ebaa4 (diff)
downloadperlweeklychallenge-club-73db1085b9c910bfa31f7c952c1ac8a76a06e09b.tar.gz
perlweeklychallenge-club-73db1085b9c910bfa31f7c952c1ac8a76a06e09b.tar.bz2
perlweeklychallenge-club-73db1085b9c910bfa31f7c952c1ac8a76a06e09b.zip
Merge pull request #12139 from simongreen-net/master
sgreen solutions to challenge 324
-rw-r--r--challenge-324/sgreen/README.md4
-rw-r--r--challenge-324/sgreen/blog.txt1
-rw-r--r--challenge-324/sgreen/go/ch-1.go50
-rwxr-xr-xchallenge-324/sgreen/perl/ch-1.pl31
-rwxr-xr-xchallenge-324/sgreen/perl/ch-2.pl28
-rwxr-xr-xchallenge-324/sgreen/python/ch-1.py32
-rwxr-xr-xchallenge-324/sgreen/python/ch-2.py36
-rwxr-xr-xchallenge-324/sgreen/python/test.py21
8 files changed, 201 insertions, 2 deletions
diff --git a/challenge-324/sgreen/README.md b/challenge-324/sgreen/README.md
index 2e2ab9bc80..1044b4c6b8 100644
--- a/challenge-324/sgreen/README.md
+++ b/challenge-324/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 323
+# The Weekly Challenge 324
-Blog: [Counting the tax](https://dev.to/simongreennet/weekly-challenge-counting-the-tax-53fh)
+Blog: [The total array](https://dev.to/simongreennet/weekly-challenge-the-total-array-2h8k)
diff --git a/challenge-324/sgreen/blog.txt b/challenge-324/sgreen/blog.txt
new file mode 100644
index 0000000000..b948c8fa51
--- /dev/null
+++ b/challenge-324/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-the-total-array-2h8k \ No newline at end of file
diff --git a/challenge-324/sgreen/go/ch-1.go b/challenge-324/sgreen/go/ch-1.go
new file mode 100644
index 0000000000..230af3a903
--- /dev/null
+++ b/challenge-324/sgreen/go/ch-1.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+)
+
+func twodArray(ints []int, rows, cols int) ([][]int, error) {
+ if rows < 1 || cols < 1 {
+ return nil, fmt.Errorf("Rows and columns must be greater than 0.")
+ }
+ if rows*cols != len(ints) {
+ return nil, fmt.Errorf("The product of rows and columns must equal the length of the input list.")
+ }
+ result := make([][]int, rows)
+ for i := 0; i < rows; i++ {
+ row := make([]int, cols)
+ for j := 0; j < cols; j++ {
+ row[j] = ints[i*cols+j]
+ }
+ result[i] = row
+ }
+ return result, nil
+}
+
+func main() {
+ if len(os.Args) < 4 {
+ fmt.Println("Usage: go run ch-1.go <int1> <int2> ... <rows> <cols>")
+ os.Exit(1)
+ }
+ array := make([]int, len(os.Args)-1)
+ for i, arg := range os.Args[1:] {
+ n, err := strconv.Atoi(arg)
+ if err != nil {
+ fmt.Printf("Invalid integer: %s\n", arg)
+ os.Exit(1)
+ }
+ array[i] = n
+ }
+ rows := array[len(array)-2]
+ cols := array[len(array)-1]
+ ints := array[:len(array)-2]
+ result, err := twodArray(ints, rows, cols)
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ fmt.Println(result)
+} \ No newline at end of file
diff --git a/challenge-324/sgreen/perl/ch-1.pl b/challenge-324/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..26cec335b5
--- /dev/null
+++ b/challenge-324/sgreen/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ my $cols = pop @ints;
+ my $rows = pop @ints;
+ if ( $rows < 1 or $cols < 1 ) {
+ die "Rows and columns must be greater than 0.\n";
+ }
+ if ( @ints != $rows * $cols ) {
+ die
+ "The product of rows and columns must equal the length of the input list.\n";
+
+ }
+ my @result = ();
+ foreach my $row ( 0 .. $rows - 1 ) {
+ my @row = ();
+ foreach my $col ( 0 .. $cols - 1 ) {
+ push @row, shift @ints;
+ }
+ push @result, \@row;
+ }
+
+ say "(" . join( ", ", map { '[' . join( ", ", @$_ ) . ']' } @result ) . ")";
+}
+
+main(@ARGV);
diff --git a/challenge-324/sgreen/perl/ch-2.pl b/challenge-324/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..983ee77edb
--- /dev/null
+++ b/challenge-324/sgreen/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use Algorithm::Combinatorics qw(combinations);
+
+sub main (@ints) {
+ my $total = 0;
+ foreach my $i ( 1 .. scalar(@ints) ) {
+ # Generate all combinations of length i
+ # and calculate the XOR for each combination
+ my $combination = combinations( \@ints, $i );
+ while ( my $combo = $combination->next ) {
+ my $xor_value = 0;
+ foreach my $num (@$combo) {
+ $xor_value ^= $num;
+ }
+ $total += $xor_value;
+ }
+ }
+
+ say $total;
+}
+
+main(@ARGV);
diff --git a/challenge-324/sgreen/python/ch-1.py b/challenge-324/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..4053826921
--- /dev/null
+++ b/challenge-324/sgreen/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def twod_array(ints: list, rows: int, cols: int) -> list[list[int]]:
+ """
+ Convert a flat list of integers into a 2D array with specified rows and columns.
+ :param ints: List of integers to convert.
+ :param rows: Number of rows in the 2D array.
+ :param cols: Number of columns in the 2D array.
+ :return: 2D array represented as a list of lists.
+ """
+ if rows < 1 or cols < 1:
+ raise ValueError("Rows and columns must be greater than 0.")
+ if rows * cols != len(ints):
+ raise ValueError(
+ "The product of rows and columns must equal the length of the input list.")
+
+ return [[ints[i * cols + j] for j in range(cols)] for i in range(rows)]
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ *ints, rows, cols = array
+ result = twod_array(ints, rows, cols)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-324/sgreen/python/ch-2.py b/challenge-324/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d3534bb537
--- /dev/null
+++ b/challenge-324/sgreen/python/ch-2.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+import sys
+
+from itertools import combinations
+
+
+def total_xor(ints: list) -> int:
+ """
+ Calculate the total XOR of all combinations of integers in the list.
+ :param ints: List of integers to calculate the total XOR.
+ :return: Total XOR as a hexadecimal string.
+ """
+
+ total = 0
+ for i in range(1, len(ints) + 1):
+ # Generate all combinations of length i
+ # and calculate the XOR for each combination
+ for combo in combinations(ints, i):
+ xor_value = 0
+ for num in combo:
+ xor_value ^= num
+ total += xor_value
+
+ return total
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = total_xor(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-324/sgreen/python/test.py b/challenge-324/sgreen/python/test.py
new file mode 100755
index 0000000000..9203697c3b
--- /dev/null
+++ b/challenge-324/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.twod_array([1, 2, 3, 4], 2, 2), [[1, 2], [3, 4]])
+ self.assertEqual(ch_1.twod_array([1, 2, 3], 1, 3), [[1, 2, 3]])
+ self.assertEqual(ch_1.twod_array([1, 2, 3, 4], 4, 1), [[1], [2], [3], [4]])
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.total_xor([1, 3]), 6)
+ self.assertEqual(ch_2.total_xor([5, 1, 6]), 28)
+ self.assertEqual(ch_2.total_xor([3, 4, 5, 6, 7, 8]), 480)
+
+
+if __name__ == '__main__':
+ unittest.main()