diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-03-04 00:07:21 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-03-04 00:07:21 +0000 |
| commit | 40d47c8ead708860c4e3f21bae091506a42807f4 (patch) | |
| tree | f3afcf3e43492360076f7462c4466ae48df694dd | |
| parent | 80a29b3bc4529d81f1a709fabab5ea1900eb679d (diff) | |
| parent | b5df38e77ecab31f91d505c4842ea3ae8c6a98ad (diff) | |
| download | perlweeklychallenge-club-40d47c8ead708860c4e3f21bae091506a42807f4.tar.gz perlweeklychallenge-club-40d47c8ead708860c4e3f21bae091506a42807f4.tar.bz2 perlweeklychallenge-club-40d47c8ead708860c4e3f21bae091506a42807f4.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -rw-r--r-- | challenge-257/sgreen/README.md | 2 | ||||
| -rw-r--r-- | challenge-258/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-258/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-258/sgreen/perl/ch-1.pl | 15 | ||||
| -rwxr-xr-x | challenge-258/sgreen/perl/ch-2.pl | 25 | ||||
| -rwxr-xr-x | challenge-258/sgreen/python/ch-1.py | 18 | ||||
| -rwxr-xr-x | challenge-258/sgreen/python/ch-2.py | 27 | ||||
| -rwxr-xr-x | challenge-258/sgreen/python/test.py | 21 |
8 files changed, 110 insertions, 3 deletions
diff --git a/challenge-257/sgreen/README.md b/challenge-257/sgreen/README.md index a8ccde13ba..5e4561f569 100644 --- a/challenge-257/sgreen/README.md +++ b/challenge-257/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 256 +# The Weekly Challenge 257 Blog: [Matching and zipping](https://dev.to/simongreennet/matching-and-zipping-3bl7) diff --git a/challenge-258/sgreen/README.md b/challenge-258/sgreen/README.md index a8ccde13ba..72cd7411bb 100644 --- a/challenge-258/sgreen/README.md +++ b/challenge-258/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 256 +# The Weekly Challenge 258 -Blog: [Matching and zipping](https://dev.to/simongreennet/matching-and-zipping-3bl7) +Blog: [Weekly Challenge 258](https://dev.to/simongreennet/weekly-challenge-258-1mg) diff --git a/challenge-258/sgreen/blog.txt b/challenge-258/sgreen/blog.txt new file mode 100644 index 0000000000..2d5732f899 --- /dev/null +++ b/challenge-258/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-258-1mg
\ No newline at end of file diff --git a/challenge-258/sgreen/perl/ch-1.pl b/challenge-258/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..1664a3798e --- /dev/null +++ b/challenge-258/sgreen/perl/ch-1.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'sum'; + +sub main (@ints) { + my $count = grep { length($_) % 2 == 0 } @ints; + say $count; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-258/sgreen/perl/ch-2.pl b/challenge-258/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..5c2aa4c2be --- /dev/null +++ b/challenge-258/sgreen/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/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 $total = 0; + while ( my ( $pos, $value ) = each @ints ) { + # Count the number of ones in the position value + my $ones = ( sprintf( '%b', $pos ) =~ tr/1/1/ ); + if ( $k == $ones ) { + # If it matches the 'k', add the value to the total + $total += $value; + } + } + + say $total; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-258/sgreen/python/ch-1.py b/challenge-258/sgreen/python/ch-1.py new file mode 100755 index 0000000000..caab0a1a85 --- /dev/null +++ b/challenge-258/sgreen/python/ch-1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import sys + + +def count_even_digits(ints: list) -> int: + return sum(1 for i in ints if len(str(i)) % 2 == 0) + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = count_even_digits(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-258/sgreen/python/ch-2.py b/challenge-258/sgreen/python/ch-2.py new file mode 100755 index 0000000000..f670d54629 --- /dev/null +++ b/challenge-258/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def sum_of_values(ints: list, k: int) -> int: + total = 0 + + for pos, value in enumerate(ints): + # Count the number of ones in the position value + if k == bin(pos).count('1'): + # If it matches the 'k', add the value to the total + total += value + + return total + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + k = array.pop() + result = sum_of_values(array, k) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-258/sgreen/python/test.py b/challenge-258/sgreen/python/test.py new file mode 100755 index 0000000000..25e8507722 --- /dev/null +++ b/challenge-258/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.count_even_digits([10, 1, 111, 24, 1000]), 3) + self.assertEqual(ch_1.count_even_digits([111, 1, 11111]), 0) + self.assertEqual(ch_1.count_even_digits([2, 8, 1024, 256]), 1) + + def test_ch_2(self): + self.assertEqual(ch_2.sum_of_values([2, 5, 9, 11, 3], 1), 17) + self.assertEqual(ch_2.sum_of_values([2, 5, 9, 11, 3], 2), 11) + self.assertEqual(ch_2.sum_of_values([2, 5, 9, 11, 3], 0), 2) + + +if __name__ == '__main__': + unittest.main() |
