aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-28 12:47:21 +0000
committerGitHub <noreply@github.com>2024-02-28 12:47:21 +0000
commitf2d2e4943deeadba02481e11819f9646110b8570 (patch)
tree7c85019e9af7f9552e1e5c9417cf80b09c2d82a5
parent6be62f2ed82dd77abee6b3b710c0e697f927dcb3 (diff)
parenteaad15f95557cf797ea0d02fad9eb7120eb63e2c (diff)
downloadperlweeklychallenge-club-f2d2e4943deeadba02481e11819f9646110b8570.tar.gz
perlweeklychallenge-club-f2d2e4943deeadba02481e11819f9646110b8570.tar.bz2
perlweeklychallenge-club-f2d2e4943deeadba02481e11819f9646110b8570.zip
Merge pull request #9669 from packy/master
Challenge 258 solutions by Packy Anderson
-rw-r--r--challenge-258/packy-anderson/README.md2
-rw-r--r--challenge-258/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-258/packy-anderson/perl/ch-1.pl26
-rwxr-xr-xchallenge-258/packy-anderson/perl/ch-2.pl37
-rwxr-xr-xchallenge-258/packy-anderson/python/ch-1.py25
-rwxr-xr-xchallenge-258/packy-anderson/python/ch-2.py34
-rwxr-xr-xchallenge-258/packy-anderson/raku/ch-1.raku24
-rwxr-xr-xchallenge-258/packy-anderson/raku/ch-2.raku36
8 files changed, 184 insertions, 1 deletions
diff --git a/challenge-258/packy-anderson/README.md b/challenge-258/packy-anderson/README.md
index c576612b58..64879ee70c 100644
--- a/challenge-258/packy-anderson/README.md
+++ b/challenge-258/packy-anderson/README.md
@@ -16,4 +16,4 @@
## Blog Post
-[Merge the Maximum String Pairs](https://packy.dardan.com/b/Hg)
+[Even Digits have a Sum!](https://packy.dardan.com/b/J8)
diff --git a/challenge-258/packy-anderson/blog.txt b/challenge-258/packy-anderson/blog.txt
new file mode 100644
index 0000000000..52f364572b
--- /dev/null
+++ b/challenge-258/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/J8 \ No newline at end of file
diff --git a/challenge-258/packy-anderson/perl/ch-1.pl b/challenge-258/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..a79c22da57
--- /dev/null
+++ b/challenge-258/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use POSIX qw( log10 floor );
+
+sub evenDigitCount(@ints) {
+ my $count = 0; # in case there are no even digit ints
+ foreach my $n ( @ints ) {
+ $count++ if floor(log10($n) + 1) % 2 == 0;
+ }
+ return $count;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' . join(', ', @ints) . ')';
+ say 'Output: ' . evenDigitCount(@ints);
+}
+
+say "Example 1:";
+solution(10, 1, 111, 24, 1000);
+
+say "\nExample 2:";
+solution(111, 1, 11111);
+
+say "\nExample 3:";
+solution(2, 8, 1024, 256);
diff --git a/challenge-258/packy-anderson/perl/ch-2.pl b/challenge-258/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..228cb7c9dc
--- /dev/null
+++ b/challenge-258/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+use v5.38;
+
+use Memoize;
+memoize('setBitCount');
+
+sub setBitCount($i) {
+ my $count = 0;
+ my $bit = 1;
+ while ($bit <= $i) {
+ $count++ if $i & $bit; # count if we have this bit set
+ $bit <<= 1; # shift bits left, ie 10 becomes 100
+ }
+ return $count;
+}
+
+sub valueSum($k, @ints) {
+ my $sum = 0;
+ foreach my $i ( 0 .. $#ints ) {
+ $sum += $ints[$i] if setBitCount($i) == $k;
+ }
+ return $sum;
+}
+
+sub solution($k, $ints) {
+ say 'Input: @ints = (' . join(', ', @$ints) . '), $k = ' . $k;
+ say 'Output: ' . valueSum($k, @$ints);
+}
+
+say "Example 1:";
+solution(1, [2, 5, 9, 11, 3]);
+
+say "\nExample 2:";
+solution(2, [2, 5, 9, 11, 3]);
+
+say "\nExample 3:";
+solution(0, [2, 5, 9, 11, 3]);
diff --git a/challenge-258/packy-anderson/python/ch-1.py b/challenge-258/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..fb5188ace1
--- /dev/null
+++ b/challenge-258/packy-anderson/python/ch-1.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+
+from math import floor, log10
+
+def evenDigitCount(ints):
+ count = 0; # in case there are no even digit ints
+ for n in ints:
+ if floor(log10(n) + 1) % 2 == 0: count += 1
+ return count
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(ints):
+ print(f'Input: @arr = ({comma_join(ints)})')
+ print(f'Output: {evenDigitCount(ints)}')
+
+print('Example 1:')
+solution([10, 1, 111, 24, 1000])
+
+print('\nExample 2:')
+solution([111, 1, 11111])
+
+print('\nExample 3:')
+solution([2, 8, 1024, 256])
diff --git a/challenge-258/packy-anderson/python/ch-2.py b/challenge-258/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..b9bdce3bb0
--- /dev/null
+++ b/challenge-258/packy-anderson/python/ch-2.py
@@ -0,0 +1,34 @@
+#!/usr/bin/env python
+
+from functools import cache
+
+@cache
+def setBitCount(i):
+ count = 0
+ bit = 1
+ while (bit <= i):
+ if i & bit: count += 1 # count if we have this bit set
+ bit <<= 1 # shift bits left, ie 10 becomes 100
+ return count
+
+def valueSum(k, ints):
+ sum = 0
+ for i in range(len(ints)):
+ if setBitCount(i) == k: sum += ints[i]
+ return sum
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(k, ints):
+ print(f'Input: @ints = ({comma_join(ints)}), $k = {k}')
+ print(f'Output: {valueSum(k, ints)}')
+
+print('Example 1:')
+solution(1, [2, 5, 9, 11, 3])
+
+print('\nExample 2:')
+solution(2, [2, 5, 9, 11, 3])
+
+print('\nExample 3:')
+solution(0, [2, 5, 9, 11, 3])
diff --git a/challenge-258/packy-anderson/raku/ch-1.raku b/challenge-258/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..7307ae308e
--- /dev/null
+++ b/challenge-258/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+use v6;
+
+sub evenDigitCount(@ints) {
+ my $count = 0; # in case there are no even digit ints
+ for @ints -> $n {
+ $count++ if floor(log10($n) + 1) % 2 == 0;
+ }
+ return $count;
+}
+
+sub solution(@ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ ')';
+ say 'Output: ' ~ evenDigitCount(@ints);
+}
+
+say "Example 1:";
+solution([10, 1, 111, 24, 1000]);
+
+say "\nExample 2:";
+solution([111, 1, 11111]);
+
+say "\nExample 3:";
+solution([2, 8, 1024, 256]);
diff --git a/challenge-258/packy-anderson/raku/ch-2.raku b/challenge-258/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..2480cad5d5
--- /dev/null
+++ b/challenge-258/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,36 @@
+#!/usr/bin/env raku
+use v6;
+
+use experimental :cached;
+
+sub setBitCount($i) is cached {
+ my $count = 0;
+ my $bit = 1;
+ while ($bit <= $i) {
+ $count++ if $i +& $bit; # count if we have this bit set
+ $bit +<= 1; # shift bits left, ie 10 becomes 100
+ }
+ return $count;
+}
+
+sub valueSum($k, @ints) {
+ my $sum = 0;
+ for 0 .. @ints.end -> $i {
+ $sum += @ints[$i] if setBitCount($i) == $k;
+ }
+ return $sum;
+}
+
+sub solution($k, @ints) {
+ say 'Input: @ints = (' ~ @ints.join(', ') ~ '), $k = ' ~ $k;
+ say 'Output: ' ~ valueSum($k, @ints);
+}
+
+say "Example 1:";
+solution(1, [2, 5, 9, 11, 3]);
+
+say "\nExample 2:";
+solution(2, [2, 5, 9, 11, 3]);
+
+say "\nExample 3:";
+solution(0, [2, 5, 9, 11, 3]);