diff options
| author | Simon Green <mail@simon.green> | 2025-06-08 17:43:36 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-06-08 17:43:36 +1000 |
| commit | f176e0edb6317066bbec46ccc940b313892ebaa4 (patch) | |
| tree | 7257be550c6b70ff6570b4c438a0fe63bf1c2974 /challenge-324/sgreen/python | |
| parent | b0159e77cc4e56da3a5a1c86d5769c652bcc887e (diff) | |
| download | perlweeklychallenge-club-f176e0edb6317066bbec46ccc940b313892ebaa4.tar.gz perlweeklychallenge-club-f176e0edb6317066bbec46ccc940b313892ebaa4.tar.bz2 perlweeklychallenge-club-f176e0edb6317066bbec46ccc940b313892ebaa4.zip | |
sgreen solutions to challenge 324
Diffstat (limited to 'challenge-324/sgreen/python')
| -rwxr-xr-x | challenge-324/sgreen/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-324/sgreen/python/ch-2.py | 36 | ||||
| -rwxr-xr-x | challenge-324/sgreen/python/test.py | 21 |
3 files changed, 89 insertions, 0 deletions
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() |
