diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-07 10:35:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-07 10:35:40 +0100 |
| commit | e9626071ae8fae41c7b6d868050e0b68b9d6da74 (patch) | |
| tree | 78c2b4d28f963672f20ae43356ba0cee38dfb734 | |
| parent | 5a1376704f54e74f87f811c914f36aa87eb4cc77 (diff) | |
| parent | e8a06518b223e93803e1dd78d2e07c92c15b8115 (diff) | |
| download | perlweeklychallenge-club-e9626071ae8fae41c7b6d868050e0b68b9d6da74.tar.gz perlweeklychallenge-club-e9626071ae8fae41c7b6d868050e0b68b9d6da74.tar.bz2 perlweeklychallenge-club-e9626071ae8fae41c7b6d868050e0b68b9d6da74.zip | |
Merge pull request #9885 from simongreen-net/master
sgreen solutions to challenge 263
| -rw-r--r-- | challenge-263/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-263/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-263/sgreen/perl/ch-1.pl | 21 | ||||
| -rwxr-xr-x | challenge-263/sgreen/perl/ch-2.pl | 36 | ||||
| -rwxr-xr-x | challenge-263/sgreen/python/ch-1.py | 23 | ||||
| -rwxr-xr-x | challenge-263/sgreen/python/ch-2.py | 30 | ||||
| -rwxr-xr-x | challenge-263/sgreen/python/test.py | 30 |
7 files changed, 143 insertions, 2 deletions
diff --git a/challenge-263/sgreen/README.md b/challenge-263/sgreen/README.md index 9a3b6396b1..933fe5f90e 100644 --- a/challenge-263/sgreen/README.md +++ b/challenge-263/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 262 +# The Weekly Challenge 263 -Blog: [The maximum divisible](https://dev.to/simongreennet/the-maximum-divisible-4mm6) +Blog: [Finding the target](https://dev.to/simongreennet/finding-the-target-26em) diff --git a/challenge-263/sgreen/blog.txt b/challenge-263/sgreen/blog.txt new file mode 100644 index 0000000000..bc7351d593 --- /dev/null +++ b/challenge-263/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/finding-the-target-26em
\ No newline at end of file diff --git a/challenge-263/sgreen/perl/ch-1.pl b/challenge-263/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..a73bc61e89 --- /dev/null +++ b/challenge-263/sgreen/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@ints) { + # The last value is $k + my $k = pop(@ints); + + # Sort the rest of the array + @ints = sort { $a <=> $b } @ints; + + # Return the indexes where the item is 'k' + my @result = grep { $ints[$_] == $k } ( 0 .. $#ints ); + + say '(', join( ', ', @result ), ')'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-263/sgreen/perl/ch-2.pl b/challenge-263/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..491df1cd59 --- /dev/null +++ b/challenge-263/sgreen/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use JSON qw(decode_json encode_json); + +sub merge_items(@arrays) { + # Calculate the total of each items + my %totals = (); + foreach my $array (@arrays) { + foreach my $item (@$array) { + my ( $item_id, $item_qty ) = @$item; + $totals{$item_id} += $item_qty; + } + } + + # Turn this into a sorted list of item_id and item_qty pairs + return [ map { [ $_, $totals{$_} ] } ( sort { $a <=> $b } keys(%totals) ) ]; +} + +sub main (@array) { + # Convert each input into a list of lists + my @inputs = (); + foreach my $json_str (@array) { + push @inputs, decode_json($json_str); + } + + # Do the challenge + my $result = merge_items(@inputs); + say encode_json($result); +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-263/sgreen/python/ch-1.py b/challenge-263/sgreen/python/ch-1.py new file mode 100755 index 0000000000..5f1e0be8fb --- /dev/null +++ b/challenge-263/sgreen/python/ch-1.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 + +import sys + + +def target_index(ints: list, k: int) -> list: + # Sort the array + ints = sorted(ints) + + # Return the indexes where the item is 'k' + return [pos for pos, value in enumerate(ints) if value == k] + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + k = array.pop() + result = target_index(array, k) + print('(' + ', '.join(map(str, result)) + ')') + + +if __name__ == '__main__': + main() diff --git a/challenge-263/sgreen/python/ch-2.py b/challenge-263/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a3a6d6a181 --- /dev/null +++ b/challenge-263/sgreen/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from collections import defaultdict +import json +import sys + + +def merge_items(*arrays) -> list: + # Calculate the total of each items + totals = defaultdict(int) + for array in arrays: + for item in array: + item_id, item_qty = item + totals[item_id] += item_qty + + # Turn this into a sorted list of item_id and item_qty pairs + return [[item, totals[item]] for item in sorted(totals)] + + +def main(): + # Convert each input into a list of lists + arrays = [] + for json_str in sys.argv[1:]: + arrays.append(json.loads(json_str)) + result = merge_items(*arrays) + print(json.dumps(result)) + + +if __name__ == '__main__': + main() diff --git a/challenge-263/sgreen/python/test.py b/challenge-263/sgreen/python/test.py new file mode 100755 index 0000000000..46adb6e558 --- /dev/null +++ b/challenge-263/sgreen/python/test.py @@ -0,0 +1,30 @@ +#!/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.target_index([1, 5, 3, 2, 4, 2], 2), [1, 2]) + self.assertEqual(ch_1.target_index([1, 2, 4, 3, 5], 6), []) + self.assertEqual(ch_1.target_index([5, 3, 2, 4, 2, 1], 4), [4]) + + def test_ch_2(self): + self.assertEqual( + ch_2.merge_items([[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]]), + [[1, 4], [2, 3], [3, 2]] + ) + self.assertEqual( + ch_2.merge_items([[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]]), + [[1, 8], [2, 3], [3, 3]] + ) + self.assertEqual( + ch_2.merge_items([[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]]), + [[1, 1], [2, 9], [3, 3]] + ) + + +if __name__ == '__main__': + unittest.main() |
