diff options
| author | Simon Green <mail@simon.green> | 2025-10-26 22:26:27 +1000 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2025-10-26 22:26:27 +1000 |
| commit | eb96a608b07b732eba8552a3ef032559dc3298a7 (patch) | |
| tree | dc8c6b87aa831634680a702d71470e7ede56b1a5 | |
| parent | 0089e40542c8edad54c99aad1b7e01bfe7050231 (diff) | |
| download | perlweeklychallenge-club-eb96a608b07b732eba8552a3ef032559dc3298a7.tar.gz perlweeklychallenge-club-eb96a608b07b732eba8552a3ef032559dc3298a7.tar.bz2 perlweeklychallenge-club-eb96a608b07b732eba8552a3ef032559dc3298a7.zip | |
sgreen solutions to challenge 344
| -rw-r--r-- | challenge-344/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-344/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-344/sgreen/perl/ch-1.pl | 55 | ||||
| -rwxr-xr-x | challenge-344/sgreen/perl/ch-2.pl | 54 | ||||
| -rwxr-xr-x | challenge-344/sgreen/python/ch-1.py | 57 | ||||
| -rwxr-xr-x | challenge-344/sgreen/python/ch-2.py | 46 | ||||
| -rwxr-xr-x | challenge-344/sgreen/python/test.py | 25 |
7 files changed, 240 insertions, 2 deletions
diff --git a/challenge-344/sgreen/README.md b/challenge-344/sgreen/README.md index dd69eee606..41cfdac46a 100644 --- a/challenge-344/sgreen/README.md +++ b/challenge-344/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 343 +# The Weekly Challenge 344 -Blog: [Absolute Champion](https://dev.to/simongreennet/weekly-challenge-absolute-champion-57cf) +Blog: [The one about arrays](https://dev.to/simongreennet/weekly-challenge-the-one-about-arrays-1h8k) diff --git a/challenge-344/sgreen/blog.txt b/challenge-344/sgreen/blog.txt new file mode 100644 index 0000000000..71de1f9f40 --- /dev/null +++ b/challenge-344/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-the-one-about-arrays-1h8k
\ No newline at end of file diff --git a/challenge-344/sgreen/perl/ch-1.pl b/challenge-344/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..eea29726aa --- /dev/null +++ b/challenge-344/sgreen/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'any'; + +sub divmod( $a, $b ) { + my $div = int( $a / $b ); + my $mod = $a % $b; + return ( $div, $mod ); +} + +sub main (@ints) { + # The last argument is $x + my $x = pop @ints; + my $remainder; + + # Check the list is single digit integers + if ( any { $_ !~ /^[0-9]$/ } @ints ) { + die "All elements in the list must be single digit integers (0-9).\n"; + } + + # Check x is a non-negative integer + if ( $x < 0 ) { + die "The integer x must be a non-negative integer.\n"; + } + + for ( my $pos = $#ints ; $pos > 0 ; $pos-- ) { + my $i = $ints[$pos]; + + # Split x into the carry forward and the last digit + ( $x, $remainder ) = divmod( $x, 10 ); + + # Add the remainder to the digit at the current position + # However we may need to carry forward if the new digit is >= 10 + my ( $add_x, $add_i ) = divmod( $i + $remainder, 10 ); + $ints[$pos] = $add_i; + $x += $add_x; + } + + # Handle the most significant digit + $ints[0] += $x; + + if ( $ints[0] >= 10 ) { + # We need to split this into separate digits + splice( @ints, 0, 1, split( //, $ints[0] ) ); + } + + say '(' . join( ', ', @ints ) . ')'; +} + +main(@ARGV); diff --git a/challenge-344/sgreen/perl/ch-2.pl b/challenge-344/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..abda7e848d --- /dev/null +++ b/challenge-344/sgreen/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Algorithm::Combinatorics 'permutations'; +use JSON 'decode_json'; + +sub flatten_array($list) { + return map { @$_ } @$list; +} + +sub compare_array ( $list1, $list2 ) { + return join( ' ', @$list1 ) eq join( ' ', @$list2 ); +} + +sub array_formation( $source, $target ) { + # Check that a solution is possible + if ( + not compare_array( + [ sort { $a <=> $b } flatten_array($source) ], + [ sort { $a <=> $b } @$target ] + ) + ) + { + return 'false'; + } + + # Generate all possible arrangements of the source lists + my $iter = permutations($source); + while ( my $perm = $iter->next ) { + if ( compare_array( [ flatten_array($perm) ], $target ) ) { + # It matches the target + return 'true'; + } + } + + # No arrangement will match the target + return 'false'; +} + +sub main () { + # Convert the first argument into a list of lists of integers + my $source = decode_json( $ARGV[0] ); + # Convert the remaining arguments into integers + my $target = [ @ARGV[ 1 .. $#ARGV ] ]; + + my $result = array_formation( $source, $target ); + say $result; +} + +main(); diff --git a/challenge-344/sgreen/python/ch-1.py b/challenge-344/sgreen/python/ch-1.py new file mode 100755 index 0000000000..d9c5eb513e --- /dev/null +++ b/challenge-344/sgreen/python/ch-1.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +import sys + + +def array_form_compute(ints: list[int], x: int) -> list[int]: + """Compute the array-form of an integer after adding x. + + Args: + ints: A list of single digit integers. + x: A non-negative integer to add to the integer represented by ints. + + Returns: + A list of single digit integers representing the array-form of the resulting integer. + """ + + # Check the list is single digit integers + if any(not isinstance(n, int) or n < 0 or n > 9 for n in ints): + raise ValueError( + "All elements in the list must be single digit integers (0-9)." + ) + + # Check x is a non-negative integer + if x < 0: + raise ValueError("The integer x must be a non-negative integer.") + + for pos in range(len(ints) - 1, 0, -1): + i = ints[pos] + + # Split x into the carry forward and the last digit + x, remainder = divmod(x, 10) + + # Add the remainder to the digit at the current position + # However we may need to carry forward if the new digit is >= 10 + add_x, add_i = divmod(i + remainder, 10) + ints[pos] = add_i + x += add_x + + # Handle the most significant digit + ints[0] += x + if ints[0] >= 10: + # We need to split this into separate digits + digits = map(int, str(ints[0])) + ints = list(digits) + ints[1:] + + return ints + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = array_form_compute(array[:-1], array[-1]) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-344/sgreen/python/ch-2.py b/challenge-344/sgreen/python/ch-2.py new file mode 100755 index 0000000000..e55a1aaf17 --- /dev/null +++ b/challenge-344/sgreen/python/ch-2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +from collections import Counter +from itertools import chain, permutations +import json +import sys + + +def array_formation(source: list[list[int]], target: list[int]) -> bool: + """Determine if the target array can be formed by concatenating the source arrays in any order. + + Args: + source: A list of lists of integers. + target: A list of integers. + + Returns: + True if the target can be formed, False otherwise. + """ + + # Check that a solution is possible + if Counter(chain.from_iterable(source)) != Counter(target): + return False + + # Generate all possible arrangements of the source lists + for perm in permutations(source): + combined = list(chain.from_iterable(perm)) + if combined == target: + # It matches the target + return True + + # No arrangement will match the target + return False + + +def main(): + # Convert the first argument into a list of lists of integers + source = json.loads(sys.argv[1]) + + # Convert the remaining arguments into integers + target = [int(n) for n in sys.argv[2:]] + result = array_formation(source, target) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-344/sgreen/python/test.py b/challenge-344/sgreen/python/test.py new file mode 100755 index 0000000000..d8d9c56b29 --- /dev/null +++ b/challenge-344/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/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.array_form_compute([1, 2, 3, 4], 12), [1, 2, 4, 6]) + self.assertEqual(ch_1.array_form_compute([2, 7, 4], 181), [4, 5, 5]) + self.assertEqual(ch_1.array_form_compute([9, 9, 9], 1), [1, 0, 0, 0]) + self.assertEqual(ch_1.array_form_compute([1, 0 ,0, 0, 0], 9999), [1, 9, 9, 9, 9]) + self.assertEqual(ch_1.array_form_compute([0], 1000), [1, 0, 0, 0]) + + def test_ch_2(self): + self.assertTrue(ch_2.array_formation([[2,3], [1], [4]], [1, 2, 3, 4])) + self.assertFalse(ch_2.array_formation([[1,3], [2,4]], [1, 2, 3, 4])) + self.assertTrue(ch_2.array_formation([[9,1], [5,8], [2]], [5, 8, 2, 9, 1])) + self.assertFalse(ch_2.array_formation([[1], [3]], [1, 2, 3])) + self.assertTrue(ch_2.array_formation([[7,4,6]], [7,4,6])) + + +if __name__ == '__main__': + unittest.main() |
