From 4e67759bd7b627440010e1c04e9b3ad56c9fcc1f Mon Sep 17 00:00:00 2001 From: Stephen Lynn Date: Wed, 28 Feb 2024 18:23:10 +0800 Subject: pwc 258 --- challenge-258/steve-g-lynn/blog.txt | 1 + challenge-258/steve-g-lynn/perl/ch-1.pl | 9 +++++++++ challenge-258/steve-g-lynn/perl/ch-2.pl | 33 +++++++++++++++++++++++++++++++++ challenge-258/steve-g-lynn/raku/ch-1.sh | 3 +++ challenge-258/steve-g-lynn/raku/ch-2.sh | 3 +++ 5 files changed, 49 insertions(+) create mode 100644 challenge-258/steve-g-lynn/blog.txt create mode 100644 challenge-258/steve-g-lynn/perl/ch-1.pl create mode 100644 challenge-258/steve-g-lynn/perl/ch-2.pl create mode 100755 challenge-258/steve-g-lynn/raku/ch-1.sh create mode 100755 challenge-258/steve-g-lynn/raku/ch-2.sh diff --git a/challenge-258/steve-g-lynn/blog.txt b/challenge-258/steve-g-lynn/blog.txt new file mode 100644 index 0000000000..cb6aa3d79a --- /dev/null +++ b/challenge-258/steve-g-lynn/blog.txt @@ -0,0 +1 @@ +https://thiujiac.blogspot.com/2024/02/pwc-258.html diff --git a/challenge-258/steve-g-lynn/perl/ch-1.pl b/challenge-258/steve-g-lynn/perl/ch-1.pl new file mode 100644 index 0000000000..07e85dc756 --- /dev/null +++ b/challenge-258/steve-g-lynn/perl/ch-1.pl @@ -0,0 +1,9 @@ +# Perl 4.019 on DOSBOX + +sub count_even_digits { + scalar( grep( ((length($_) % 2) == 0), @_) ); +} + +print &count_even_digits(10,1,111,24,1000),"\n"; #3 +print &count_even_digits(111,1,11111),"\n"; #0 +print &count_even_digits(2,8,1024,256),"\n"; #1 diff --git a/challenge-258/steve-g-lynn/perl/ch-2.pl b/challenge-258/steve-g-lynn/perl/ch-2.pl new file mode 100644 index 0000000000..60926ba13b --- /dev/null +++ b/challenge-258/steve-g-lynn/perl/ch-2.pl @@ -0,0 +1,33 @@ +#perl 4.019 on DOSBOX + +#get binary number using vec(.). +#only works for indexes up to 9 +# + +print &sum_of_values(1,2,5,9,11,3),"\n"; #17 +print &sum_of_values(2,2,5,9,11,3),"\n"; #11 +print &sum_of_values(0,2,5,9,11,3),"\n"; #2 + +sub sum_of_values { + local($k,@ints)=@_; + ($#ints > 9) && (return "Error. Max 10 elements allowed."); + grep($_ =~ /[^0-9]/, ($k,@ints)) && (return "Error. Only integers allowed."); + local($sum_of_values)=0; + foreach (0 .. $#ints) { + (&binsum($_)==$k) && ($sum_of_values += $ints[$_]); + } + $sum_of_values; +} + + +sub binsum { + #sum of digits of binary form of a number + local($num)=$_[0]; + local($binsum)=0; + foreach (0 .. 3) { + $binsum += vec($num,$_,1); + } + $binsum; +} + + diff --git a/challenge-258/steve-g-lynn/raku/ch-1.sh b/challenge-258/steve-g-lynn/raku/ch-1.sh new file mode 100755 index 0000000000..c062dad9e8 --- /dev/null +++ b/challenge-258/steve-g-lynn/raku/ch-1.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -e 'say +@*ARGS.grep({$_.chars %% 2})' $@ diff --git a/challenge-258/steve-g-lynn/raku/ch-2.sh b/challenge-258/steve-g-lynn/raku/ch-2.sh new file mode 100755 index 0000000000..e6e0c5785c --- /dev/null +++ b/challenge-258/steve-g-lynn/raku/ch-2.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -e 'my ($k, @ints)=@*ARGS; say sum @ints[ (0 .. @ints.elems-1).grep({$_.base(2).comb.sum == $k}) ];' $@ -- cgit