diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-04 00:15:36 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-04 00:15:36 +0000 |
| commit | 0478d96b0dfdf673f15eddcde2d6ef2b8d4c85b4 (patch) | |
| tree | e48669d5f24872df45091e6bac3043cf629867c3 | |
| parent | c80d623298c13f47f046d4b2dcfc321af9165914 (diff) | |
| parent | 5e8cc7cf8b4cc5260c6d16c7453f4b4a0646465e (diff) | |
| download | perlweeklychallenge-club-0478d96b0dfdf673f15eddcde2d6ef2b8d4c85b4.tar.gz perlweeklychallenge-club-0478d96b0dfdf673f15eddcde2d6ef2b8d4c85b4.tar.bz2 perlweeklychallenge-club-0478d96b0dfdf673f15eddcde2d6ef2b8d4c85b4.zip | |
Merge pull request #9685 from Util/c258
Add TWC 258 solutions by Bruce Gray, in Raku, Perl, and Java.
| -rw-r--r-- | challenge-258/bruce-gray/java/ch-1.java | 34 | ||||
| -rw-r--r-- | challenge-258/bruce-gray/java/ch-2.java | 41 | ||||
| -rw-r--r-- | challenge-258/bruce-gray/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-258/bruce-gray/perl/ch-2.pl | 22 | ||||
| -rw-r--r-- | challenge-258/bruce-gray/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-258/bruce-gray/raku/ch-2.raku | 33 |
6 files changed, 159 insertions, 0 deletions
diff --git a/challenge-258/bruce-gray/java/ch-1.java b/challenge-258/bruce-gray/java/ch-1.java new file mode 100644 index 0000000000..b60b8fcdf4 --- /dev/null +++ b/challenge-258/bruce-gray/java/ch-1.java @@ -0,0 +1,34 @@ +// java java/ch-1.java +import java.util.List; +public class ch_1 { + + + static long task1 ( List<Integer> ns ) { + return ns.stream() + .filter(n -> n.toString().length() % 2 == 0) + .count(); + } + + + // Test harness + static int test_number = 1; + static void plan ( Integer count ) { System.out.printf("1..%d\n", count); } + static boolean is ( long got, long expected, String desc ) { + boolean ret = got == expected; + System.out.printf("%s %d - %s\n", (ret ? "ok" : "not ok"), test_number++, desc); + return ret; + } + public static void main(String[] args) { + record Testcase ( int expected, List<Integer> inputs ) {} + + List<Testcase> tst = List.of( + new Testcase( 3, List.of( 10, 1, 111, 24, 1000 ) ), + new Testcase( 0, List.of( 111, 1, 11111 ) ), + new Testcase( 1, List.of( 2, 8, 1024, 256 ) ) + ); + plan(tst.size()); + for (Testcase t : tst) { + is( task1(t.inputs), t.expected, t.inputs.toString() ); + } + } +} diff --git a/challenge-258/bruce-gray/java/ch-2.java b/challenge-258/bruce-gray/java/ch-2.java new file mode 100644 index 0000000000..f7f9036d89 --- /dev/null +++ b/challenge-258/bruce-gray/java/ch-2.java @@ -0,0 +1,41 @@ +// java java/ch-2.java +import java.util.List; +import java.util.stream.IntStream; +import java.util.stream.Collectors; +public class ch_2 { + + + static int task2 ( Integer k, List<Integer> ns ) { + Integer[] ints = ns.toArray(new Integer[0]); + + return IntStream.range(0, ints.length) + .filter(i -> Integer.bitCount(i) == k) + .mapToObj(i -> ints[i]) + .collect(Collectors.summingInt(Integer::intValue)); + } + + + + // Test harness + static int test_number = 1; + static void plan ( Integer count ) { System.out.printf("1..%d\n", count); } + static boolean is ( Integer got, Integer expected, String desc ) { + boolean ret = got == expected; + System.out.printf("%s %d - %s\n", (ret ? "ok" : "not ok"), test_number++, desc); + return ret; + } + public static void main(String[] args) { + record Testcase ( Integer expected, Integer in_k, List<Integer> in_nums ) {} + + List<Testcase> tst = List.of( + new Testcase( 17, 1, List.of(2, 5, 9, 11, 3) ), + new Testcase( 11, 2, List.of(2, 5, 9, 11, 3) ), + new Testcase( 2, 0, List.of(2, 5, 9, 11, 3) ) + ); + plan(tst.size()); + for (Testcase t : tst) { + is( task2(t.in_k, t.in_nums), t.expected, "k=" + t.in_k); + } + + } +} diff --git a/challenge-258/bruce-gray/perl/ch-1.pl b/challenge-258/bruce-gray/perl/ch-1.pl new file mode 100644 index 0000000000..98ab3129d5 --- /dev/null +++ b/challenge-258/bruce-gray/perl/ch-1.pl @@ -0,0 +1,16 @@ +use v5.36; +sub task1 ( @ns ) { + return 0+grep { length(abs $_) % 2 == 0 } @ns; +} + + +my @tests = ( + [ 3, [ 10, 1, 111, 24, 1000 ] ], + [ 0, [ 111, 1, 11111 ] ], + [ 1, [ 2, 8, 1024, 256 ] ], +); +use Test::More; plan tests => 0+@tests; +for (@tests) { + my ($expected, $in) = @{$_}; + is task1(@{$in}), $expected; +} diff --git a/challenge-258/bruce-gray/perl/ch-2.pl b/challenge-258/bruce-gray/perl/ch-2.pl new file mode 100644 index 0000000000..851d62d479 --- /dev/null +++ b/challenge-258/bruce-gray/perl/ch-2.pl @@ -0,0 +1,22 @@ +use v5.36; +use List::Util qw<sum>; + +sub pop_count ($n) { sprintf( '%b', $n ) =~ tr/1// } + +sub task2 ( $k, @ns ) { + my @wanted_keys = grep { pop_count($_) == $k } keys @ns; + + return sum @ns[ @wanted_keys ]; +} + + +my @tests = ( + [ 17, 1, [2, 5, 9, 11, 3] ], + [ 11, 2, [2, 5, 9, 11, 3] ], + [ 2, 0, [2, 5, 9, 11, 3] ], +); +use Test::More; plan tests => 0+@tests; +for (@tests) { + my ($expected, $in_k, $in_ints) = @{$_}; + is task2($in_k, @{$in_ints}), $expected; +} diff --git a/challenge-258/bruce-gray/raku/ch-1.raku b/challenge-258/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..c11d9222a8 --- /dev/null +++ b/challenge-258/bruce-gray/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub task1 ( @ns --> UInt ) { + return +grep { .abs.chars %% 2 }, @ns; +} + + +use Test; plan +my @tests = + ( 3, ( 10, 1, 111, 24, 1000 ) ), + ( 0, ( 111, 1, 11111 ) ), + ( 1, ( 2, 8, 1024, 256 ) ), +; +for @tests -> ( $expected, @in ) { + is task1(@in), $expected; +} diff --git a/challenge-258/bruce-gray/raku/ch-2.raku b/challenge-258/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..e20d65b1b3 --- /dev/null +++ b/challenge-258/bruce-gray/raku/ch-2.raku @@ -0,0 +1,33 @@ +sub pop-count { $^n.base(2).comb('1').elems } + +sub pop-count-fast ($n is copy) { + constant @bits_per_2bytes = [X+] (0,1) xx 16; # Bwaahaahaa! + my $c = 0; + while $n { + $c += @bits_per_2bytes[$n +& 0xFFFF]; + $n +>= 16; + } + return $c; +} + +sub task2 ( UInt $k, @ns --> UInt ) { + # More concise alternatives: + # return @ns.grep({ pop-count($++) == $k }).sum; + # return @ns.pairs.map({ .value if pop-count(.key) == $k }).sum; + + my &pop-count-is-k = *.&pop-count == $k; + + my @wanted_keys = @ns.keys.grep: &pop-count-is-k; + + return @ns[ @wanted_keys ].sum; +} + + +use Test; plan +my @tests = + ( 17, 1, (2, 5, 9, 11, 3) ), + ( 11, 2, (2, 5, 9, 11, 3) ), + ( 2, 0, (2, 5, 9, 11, 3) ), +; +for @tests -> ( $expected, $in_k, @in_ints ) { + is task2($in_k, @in_ints), $expected; +} |
