diff options
| -rwxr-xr-x | challenge-258/nelo-tovar/bash/ch-1.sh | 33 | ||||
| -rwxr-xr-x | challenge-258/nelo-tovar/bash/ch-2.sh | 79 | ||||
| -rw-r--r-- | challenge-258/nelo-tovar/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-258/nelo-tovar/perl/ch-2.pl | 47 |
4 files changed, 192 insertions, 0 deletions
diff --git a/challenge-258/nelo-tovar/bash/ch-1.sh b/challenge-258/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..ff5fd5c331 --- /dev/null +++ b/challenge-258/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 258 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +# +# Task 1 : Count Even Digits Number + +function count_even_digits_number() { + local nums=("$@") + local counter=0 + + for i in ${nums[@]}; do + if [[ $((${#i} % 2)) -eq 0 ]]; then + ((counter++)) + fi + done + + echo $counter +} + +example1='10 1 111 24 1000' +example2='111 1 11111' +example3='2 8 1024 256' + +for e in "$example1" "$example2" "$example3"; do + array=($e) + cegn=($(count_even_digits_number "${array[@]}")) + echo "Input : ints = (${array[@]})" + echo "Output : $cegn" + echo "" +done + diff --git a/challenge-258/nelo-tovar/bash/ch-2.sh b/challenge-258/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..4d36064128 --- /dev/null +++ b/challenge-258/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 258 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +# +# Task 2 : Sum of Values + +function combinations() { + local -a results=() + let idx=$1 + shift + local list=("$@") + local length=${#list[@]} + + for (( j = 0; j < $length; j++ )); do + if (( idx % 2 )); then + results=("${results[@]}" "${list[$j]}"); + fi + let idx\>\>=1 + done + echo "${results[@]}" +} + +function min() { + local array=("$@") + local min=${array[0]} + + for n in ${array[@]}; do + if [[ n -lt $min ]]; then + min=$n + fi + done + + echo $min +} + +function max() { + local array=("$@") + local max=${array[0]} + + for n in ${array[@]}; do + if [[ n -gt $max ]]; then + max=$n + fi + done + + echo $max +} + +function sum_of_values() { + local k=$1 + shift + local ints=("$@") + local length=${#ints[@]} + local sum=0 + + for (( i = 0; i < $length; i++ )); do + bits=$(bc <<< "obase=2;$i") + one_bits=${bits//0} + if [[ ${#one_bits} -eq $k ]]; then + ((sum+=${ints[$i]})) + fi + done + + echo $sum +} + +example1=('2 5 9 11 3' '2 5 9 11 3' '2 5 9 11 3') +examples_k=(1 2 0) + +for (( e = 0; e < ${#examples_k[@]}; e++ )); do + array=(${example1[$e]}) + k=${examples_k[$e]} + sov=$(sum_of_values $k "${array[@]}") + echo "Input : nums = (${array[@]}), k = $k" + echo -e "Output : $sov\n" +done + diff --git a/challenge-258/nelo-tovar/perl/ch-1.pl b/challenge-258/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..7014eca3b8 --- /dev/null +++ b/challenge-258/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 258 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +# +# Task 1 Count Even Digits Number +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 10, 1, 111, 24, 1000 ], + [ 111, 1, 11111 ], + [ 2, 8, 1024, 256 ], +); + +sub count_even_digits_number { + my $nums = shift; + + return grep {(length($_) % 2) eq 0} @$nums; +} + +for my $elements (@examples) { + my $cedn = count_even_digits_number $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $cedn; + say ' '; +} diff --git a/challenge-258/nelo-tovar/perl/ch-2.pl b/challenge-258/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..f77db0a4bb --- /dev/null +++ b/challenge-258/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 258 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-258/ +# +# Task 2 - Sum of Values +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 2, 5, 9, 11, 3 ], + [ 2, 5, 9, 11, 3 ], + [ 2, 5, 9, 11, 3 ], +); + +my @examples_k = ( 1, 2, 0); + +sub sum_of_values { + my $ints = shift; + my $k = shift; + my $length = scalar @$ints; + my $sum = 0; + + for (my $i = 0; $i < $length; $i++) { + my @bits = split(//, sprintf("%b", $i)); + my $one_bits_counter = grep {$_ eq 1} @bits; + + $sum += $ints->[$i] if ($one_bits_counter eq $k); + } + + return $sum; +} + +for (my $i = 0; $i < scalar @examples_k; $i++) { + my $elements = $examples[$i]; + my $k = $examples_k[$i]; + my $sov = sum_of_values $elements, $k; + + say 'Input : @ints = ', dump(@$elements), ', $k = ', $k; + say 'Output : ', $sov; + say ' '; +} |
