diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-05-15 16:45:20 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-15 16:45:20 -0400 |
| commit | efc2a652edb6436965e12883bedf035ea081284d (patch) | |
| tree | 7e9940d4f72fe1e8d984b3746af2d30dff96330b /challenge-320/sgreen | |
| parent | 6886bd3e06ccf8b0a564f3c434cd53131d126f70 (diff) | |
| parent | ed7588dadb77d63adca3d3aefc6cd325164e0947 (diff) | |
| download | perlweeklychallenge-club-efc2a652edb6436965e12883bedf035ea081284d.tar.gz perlweeklychallenge-club-efc2a652edb6436965e12883bedf035ea081284d.tar.bz2 perlweeklychallenge-club-efc2a652edb6436965e12883bedf035ea081284d.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-320/sgreen')
| -rw-r--r-- | challenge-320/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-320/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-320/sgreen/perl/ch-1.pl | 19 | ||||
| -rwxr-xr-x | challenge-320/sgreen/perl/ch-2.pl | 30 | ||||
| -rwxr-xr-x | challenge-320/sgreen/python/ch-1.py | 31 | ||||
| -rwxr-xr-x | challenge-320/sgreen/python/ch-2.py | 39 | ||||
| -rwxr-xr-x | challenge-320/sgreen/python/test.py | 21 |
7 files changed, 143 insertions, 2 deletions
diff --git a/challenge-320/sgreen/README.md b/challenge-320/sgreen/README.md index 2be75d9b36..030723f69a 100644 --- a/challenge-320/sgreen/README.md +++ b/challenge-320/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 319 +# The Weekly Challenge 320 -Blog: [Words and numbers](https://dev.to/simongreennet/words-and-numbers-54k6) +Blog: [Words and numbers](https://dev.to/simongreennet/weekly-challenge-the-maximum-difference-2bnm) diff --git a/challenge-320/sgreen/blog.txt b/challenge-320/sgreen/blog.txt new file mode 100644 index 0000000000..ea76ea7ea2 --- /dev/null +++ b/challenge-320/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-the-maximum-difference-2bnm
\ No newline at end of file diff --git a/challenge-320/sgreen/perl/ch-1.pl b/challenge-320/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..4e4a667069 --- /dev/null +++ b/challenge-320/sgreen/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'max'; + +sub main (@ints) { + # Count the number of positive and negative integers + my $pos_count = grep { $_ > 0 } @ints; + my $neg_count = grep { $_ < 0 } @ints; + + # Return the maximum of these two values + say max( $pos_count, $neg_count ); +} + +main(@ARGV); diff --git a/challenge-320/sgreen/perl/ch-2.pl b/challenge-320/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..0451ed4dc5 --- /dev/null +++ b/challenge-320/sgreen/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util qw(any sum); + +sub main (@ints) { + my $difference = 0; + + # Check we are only given positive integers + if ( any { $_ < 0 } @ints ) { + die "Only positive integers allowed"; + } + + foreach my $i (@ints) { + # Single digit integers have no difference + if ( $i > 9 ) { + # Calculate the difference between the number and the sum of the + # individual digits + $difference += $i - sum( split( //, $i ) ); + } + } + + say abs($difference); +} + +main(@ARGV); diff --git a/challenge-320/sgreen/python/ch-1.py b/challenge-320/sgreen/python/ch-1.py new file mode 100755 index 0000000000..60156de33e --- /dev/null +++ b/challenge-320/sgreen/python/ch-1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import sys + + +def maximum_count(ints: list) -> int: + """Return the maximum between the number of positive and negative integers + + Args: + ints (list): A list of integers + + Returns: + int: The maximum + """ + + # Count the number of positive and negative integers + pos_count = sum(1 for i in ints if i > 0) + neg_count = sum(1 for i in ints if i < 0) + + # Return the maximum of these two values + return max(pos_count, neg_count) + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = maximum_count(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-320/sgreen/python/ch-2.py b/challenge-320/sgreen/python/ch-2.py new file mode 100755 index 0000000000..cda60d3d8d --- /dev/null +++ b/challenge-320/sgreen/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +import sys + + +def sum_difference(ints: list) -> int: + """Calculate the absolute difference between digit sum and element + sum of the given list. + + Args: + ints (list): A list of positive integers + + Returns: + int: The absolute difference. + """ + # Check we are only given positive integers + if any(i < 1 for i in ints): + raise ValueError('Only positive integers allowed') + + difference = 0 + for i in ints: + # Single digit integers have no difference + if i > 9: + # Calculate the difference between the number and the sum of the + # individual digits + difference += i - sum(int(d) for d in str(i)) + + return abs(difference) + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = sum_difference(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-320/sgreen/python/test.py b/challenge-320/sgreen/python/test.py new file mode 100755 index 0000000000..81900ad922 --- /dev/null +++ b/challenge-320/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.maximum_count([-3, -2, -1, 1, 2, 3]), 3) + self.assertEqual(ch_1.maximum_count([-2, -1, 0, 0, 1]), 2) + self.assertEqual(ch_1.maximum_count([1, 2, 3, 4]), 4) + + def test_ch_2(self): + self.assertEqual(ch_2.sum_difference([1, 23, 4, 5]), 18) + self.assertEqual(ch_2.sum_difference([1, 2, 3, 4, 5]), 0) + self.assertEqual(ch_2.sum_difference([1, 2, 34]), 27) + + +if __name__ == '__main__': + unittest.main() |
