diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-31 13:39:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-31 13:39:00 +0100 |
| commit | 92e9a75b20d0063cc04a8dfd3af244f64f861bd4 (patch) | |
| tree | 9c3a60c452b0525bef9adb99fafbea09d94dbf1b | |
| parent | ffdfa5d6eb16e02e9b37eb4bd977e2e00ba800eb (diff) | |
| parent | 4c42510de23a775a8cd514185d2d4cf319f9f10a (diff) | |
| download | perlweeklychallenge-club-92e9a75b20d0063cc04a8dfd3af244f64f861bd4.tar.gz perlweeklychallenge-club-92e9a75b20d0063cc04a8dfd3af244f64f861bd4.tar.bz2 perlweeklychallenge-club-92e9a75b20d0063cc04a8dfd3af244f64f861bd4.zip | |
Merge pull request #9838 from simongreen-net/master
sgreen solutions to challenge 262
| -rw-r--r-- | challenge-262/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-262/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-262/sgreen/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-262/sgreen/perl/ch-2.pl | 24 | ||||
| -rwxr-xr-x | challenge-262/sgreen/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-262/sgreen/python/ch-2.py | 36 | ||||
| -rwxr-xr-x | challenge-262/sgreen/python/test.py | 20 |
7 files changed, 141 insertions, 2 deletions
diff --git a/challenge-262/sgreen/README.md b/challenge-262/sgreen/README.md index f28ff5feac..9a3b6396b1 100644 --- a/challenge-262/sgreen/README.md +++ b/challenge-262/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 261 +# The Weekly Challenge 262 -Blog: [Weekly Challenge 261](https://dev.to/simongreennet/weekly-challenge-261-3pg1) +Blog: [The maximum divisible](https://dev.to/simongreennet/the-maximum-divisible-4mm6) diff --git a/challenge-262/sgreen/blog.txt b/challenge-262/sgreen/blog.txt new file mode 100644 index 0000000000..10191bdd17 --- /dev/null +++ b/challenge-262/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-maximum-divisible-4mm6
\ No newline at end of file diff --git a/challenge-262/sgreen/perl/ch-1.pl b/challenge-262/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..71264d128f --- /dev/null +++ b/challenge-262/sgreen/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'max'; + +sub main (@ints) { + my $neg_count = my $pos_count = 0; + foreach my $i (@ints) { + if ( $i < 0 ) { + ++$neg_count; + } + elsif ( $i > 0 ) { + ++$pos_count; + } + } + + say max( $neg_count, $pos_count ); +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-262/sgreen/perl/ch-2.pl b/challenge-262/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..c3c5a79c95 --- /dev/null +++ b/challenge-262/sgreen/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/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); + + my $count = 0; + foreach my $i ( 0 .. $#ints - 1 ) { + foreach my $j ( $i + 1 .. $#ints ) { + if ( $ints[$i] == $ints[$j] and $i * $j % $k == 0 ) { + ++$count; + } + } + } + + say $count; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-262/sgreen/python/ch-1.py b/challenge-262/sgreen/python/ch-1.py new file mode 100755 index 0000000000..c1f34a8db7 --- /dev/null +++ b/challenge-262/sgreen/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import sys + + +def max_type(ints: list) -> int: + """Count the maximum number of positive or negative integers + + Args: + ints (list): The list + + Returns: + int: The maximum number + """ + + neg_count = pos_count = 0 + for i in ints: + if i < 0: + neg_count += 1 + elif i > 0: + pos_count += 1 + + return max(neg_count, pos_count) + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = max_type(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-262/sgreen/python/ch-2.py b/challenge-262/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a375afe037 --- /dev/null +++ b/challenge-262/sgreen/python/ch-2.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import sys + + +def equal_div(ints: list, k: int) -> int: + """Count the number of occurrences where two values are equal and the + product of their position is divisible by k + + Args: + ints (list): The input list + k (int): An integer + + Returns: + int: The number of occurrences + """ + + count = sum(1 + for i in range(len(ints)-1) + for j in range(i+1, len(ints)) + if ints[i] == ints[j] and i * j % k == 0 + ) + + return count + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + k = array.pop() + result = equal_div(array, k) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-262/sgreen/python/test.py b/challenge-262/sgreen/python/test.py new file mode 100755 index 0000000000..5a380011b6 --- /dev/null +++ b/challenge-262/sgreen/python/test.py @@ -0,0 +1,20 @@ +#!/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.max_type([-3, 1, 2, -1, 3, -2, 4]), 4) + self.assertEqual(ch_1.max_type([-1, -2, -3, 1]), 3) + self.assertEqual(ch_1.max_type([1,2]), 2) + + def test_ch_2(self): + self.assertEqual(ch_2.equal_div([3, 1, 2, 2, 2, 1, 3], 2), 4) + self.assertEqual(ch_2.equal_div([1, 2, 3], 1), 0) + + +if __name__ == '__main__': + unittest.main() |
