aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-02-27 15:51:26 +0100
committerLubos Kolouch <lubos@kolouch.net>2024-02-27 15:51:26 +0100
commitd769b31e18f4d344f4273405ee5b49bb4a55109d (patch)
tree0677be22c9c064c494d64152161254bb3da9f9f9
parent4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff)
downloadperlweeklychallenge-club-d769b31e18f4d344f4273405ee5b49bb4a55109d.tar.gz
perlweeklychallenge-club-d769b31e18f4d344f4273405ee5b49bb4a55109d.tar.bz2
perlweeklychallenge-club-d769b31e18f4d344f4273405ee5b49bb4a55109d.zip
feat(challenge-258/lubos-kolouch/perl,python,raku/): Challenge 258 LK Perl Python Raku
-rw-r--r--challenge-258/lubos-kolouch/perl/ch-1.pl24
-rw-r--r--challenge-258/lubos-kolouch/perl/ch-2.pl20
-rw-r--r--challenge-258/lubos-kolouch/python/ch-1.py31
-rw-r--r--challenge-258/lubos-kolouch/python/ch-2.py30
-rw-r--r--challenge-258/lubos-kolouch/raku/ch-1.raku20
-rw-r--r--challenge-258/lubos-kolouch/raku/ch-2.raku13
6 files changed, 138 insertions, 0 deletions
diff --git a/challenge-258/lubos-kolouch/perl/ch-1.pl b/challenge-258/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..64b3fdb3b8
--- /dev/null
+++ b/challenge-258/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub count_even_digits {
+ my (@ints) = @_;
+ my $even_digits_count = 0;
+
+ foreach my $num (@ints) {
+ if ( length( join "", split //, $num ) % 2 == 0 ) {
+ $even_digits_count++;
+ }
+ }
+
+ return $even_digits_count;
+}
+
+# Assert tests
+use Test::More;
+is( count_even_digits( 10, 1, 111, 24, 1000 ),
+ 3, "Test Case 1: Count of integers with even number of digits" );
+is( count_even_digits( 111, 1, 11111 ), 0, "Test Case 2: Count of integers with even number of digits" );
+is( count_even_digits( 2, 8, 1024, 256 ), 1, "Test Case 3: Count of integers with even number of digits" );
+done_testing();
diff --git a/challenge-258/lubos-kolouch/perl/ch-2.pl b/challenge-258/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..f5893c8098
--- /dev/null
+++ b/challenge-258/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub sum_of_values_with_k_ones {
+ my ( $arr_ref, $k ) = @_;
+ my $sum = 0;
+
+ for my $index ( 0 .. $#$arr_ref ) {
+ $sum += $arr_ref->[$index] if scalar( grep { $_ } split //, sprintf "%b", $index ) == $k;
+ }
+
+ return $sum;
+}
+
+# Testing the function with the provided examples
+my @ints = ( 2, 5, 9, 11, 3 );
+print sum_of_values_with_k_ones( \@ints, 1 ), "\n"; # Output: 17
+print sum_of_values_with_k_ones( \@ints, 2 ), "\n"; # Output: 11
+print sum_of_values_with_k_ones( \@ints, 0 ), "\n"; # Output: 2
diff --git a/challenge-258/lubos-kolouch/python/ch-1.py b/challenge-258/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..ac2db3aa67
--- /dev/null
+++ b/challenge-258/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+
+from typing import Tuple
+
+
+def count_even_digits(ints: tuple[int, ...]) -> int:
+ """
+ Counts the number of integers in the given tuple that have an even number of digits.
+
+ Parameters:
+ ints (Tuple[int, ...]): A tuple of positive integers.
+
+ Returns:
+ int: The count of integers with an even number of digits.
+ """
+ even_digits_count = 0
+ for num in ints:
+ if len(str(num)) % 2 == 0:
+ even_digits_count += 1
+ return even_digits_count
+
+
+# Assert tests
+assert count_even_digits((10, 1, 111, 24, 1000)) == 3, "Test Case 1 Failed"
+assert count_even_digits((111, 1, 11111)) == 0, "Test Case 2 Failed"
+assert count_even_digits((2, 8, 1024, 256)) == 1, "Test Case 3 Failed"
+
+# Example usage
+print(count_even_digits((10, 1, 111, 24, 1000))) # Output: 3
+print(count_even_digits((111, 1, 11111))) # Output: 0
+print(count_even_digits((2, 8, 1024, 256))) # Output: 1
diff --git a/challenge-258/lubos-kolouch/python/ch-2.py b/challenge-258/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..93cd53ddbd
--- /dev/null
+++ b/challenge-258/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+from typing import List
+
+
+def sum_of_values(ints: list[int], k: int) -> int:
+ """
+ Calculates the sum of values in the given list whose indices have exactly k 1-bits in their binary representation.
+
+ :param ints: A list of integers.
+ :param k: The number of 1-bits in the binary representation of the index.
+ :return: The sum of values whose indices have exactly k 1-bits.
+ """
+ total_sum = 0
+ for i, value in enumerate(ints):
+ if bin(i).count("1") == k:
+ total_sum += value
+ return total_sum
+
+
+# Test cases
+def test_sum_of_values():
+ assert sum_of_values([2, 5, 9, 11, 3], 1) == 17
+ assert sum_of_values([2, 5, 9, 11, 3], 2) == 11
+ assert sum_of_values([2, 5, 9, 11, 3], 0) == 2
+ print("All tests passed.")
+
+
+if __name__ == "__main__":
+ test_sum_of_values()
diff --git a/challenge-258/lubos-kolouch/raku/ch-1.raku b/challenge-258/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..b22aea03b7
--- /dev/null
+++ b/challenge-258/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/env raku
+
+sub count-even-digits(@ints) {
+ my $even-digits-count = 0;
+
+ for @ints -> $num {
+ if $num.Str.chars % 2 == 0 {
+ $even-digits-count++;
+ }
+ }
+
+ return $even-digits-count;
+}
+
+# Assert tests
+use Test;
+is count-even-digits(10, 1, 111, 24, 1000), 3, "Test Case 1: Count of integers with even number of digits";
+is count-even-digits(111, 1, 11111), 0, "Test Case 2: Count of integers with even number of digits";
+is count-even-digits(2, 8, 1024, 256), 1, "Test Case 3: Count of integers with even number of digits";
+done-testing;
diff --git a/challenge-258/lubos-kolouch/raku/ch-2.raku b/challenge-258/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..feb984821e
--- /dev/null
+++ b/challenge-258/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,13 @@
+sub sum-of-values-with-k-ones(@arr, Int $k) {
+ my $sum = 0;
+ for 0..@arr.elems - 1 -> $index {
+ $sum += @arr[$index] if $index.base(2).comb('1').elems == $k;
+ }
+ return $sum;
+}
+
+# Testing the function with the provided examples
+my @ints = 2, 5, 9, 11, 3;
+say sum-of-values-with-k-ones(@ints, 1); # Output: 17
+say sum-of-values-with-k-ones(@ints, 2); # Output: 11
+say sum-of-values-with-k-ones(@ints, 0); # Output: 2