diff options
| author | ntovar <tovar.nelo@gmail.com> | 2024-05-18 17:50:35 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2024-05-18 17:50:35 -0500 |
| commit | c8202b4a0f2bc2d302bfdf0bcedc6561eac10639 (patch) | |
| tree | 92443e5c6da2fc088e268b2e65f47246492c1924 | |
| parent | 02e309b3d451fff60404eb5ab3e539056f99ce0d (diff) | |
| download | perlweeklychallenge-club-c8202b4a0f2bc2d302bfdf0bcedc6561eac10639.tar.gz perlweeklychallenge-club-c8202b4a0f2bc2d302bfdf0bcedc6561eac10639.tar.bz2 perlweeklychallenge-club-c8202b4a0f2bc2d302bfdf0bcedc6561eac10639.zip | |
Challenge 269. Add Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-269/nelo-tovar/bash/ch-1.sh | 35 | ||||
| -rwxr-xr-x | challenge-269/nelo-tovar/bash/ch-2.sh | 37 | ||||
| -rw-r--r-- | challenge-269/nelo-tovar/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-269/nelo-tovar/perl/ch-2.pl | 47 |
4 files changed, 161 insertions, 0 deletions
diff --git a/challenge-269/nelo-tovar/bash/ch-1.sh b/challenge-269/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..fb2848b162 --- /dev/null +++ b/challenge-269/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 269 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/ +# +# Task 1 : Bitwise OR + +function bitwise_or() { + local nums=("$@") + local length=${#nums[@]} + + for (( i = 0; i < $length - 1; i++ )); do + for (( j = $i + 1 ; j < $length; j++ )); do + k=$(( ${nums[$i]} | ${nums[$j]} )) + if [[ $(($k % 2)) -eq 0 ]]; then + echo true + exit 0 + fi + done + done + + echo false +} + +examples=('1 2 3 4 5' '2 3 8 16' '1 2 5 7 9') + +for e in ${!examples[@]}; do + array=(${examples[$e]}) + bo=($(bitwise_or "${array[@]}")) + echo "Input : inst = (${array[@]})" + echo "Output : $bo" + echo "" +done + diff --git a/challenge-269/nelo-tovar/bash/ch-2.sh b/challenge-269/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..f1f5eeeecc --- /dev/null +++ b/challenge-269/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 269 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/ +# +# Task 2 : Distribute Elements + +function distribute_elements() { + local nums=("$@") + local n=${#nums[@]} + local arr1=() + local arr2=() + + arr1+=(${nums[0]}) + arr2+=(${nums[1]}) + + for (( i = 2; i < n; i++ )); do + if [ ${arr1[-1]} -gt ${arr2[-1]} ]; then + arr1+=(${nums[$i]}) + else + arr2+=(${nums[$i]}) + fi + done + + echo "${arr1[@]} ${arr2[@]}" +} + +examples=('2 1 3 4 5' '3 2 4' '5 4 3 8' ) + +for e in ${!examples[@]}; do + array=(${examples[$e]}) + gh=$(distribute_elements "${array[@]}") + echo "Input : ints = (${array[@]})" + echo -e "Output : $gh\n" +done + diff --git a/challenge-269/nelo-tovar/perl/ch-1.pl b/challenge-269/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..46ad249a38 --- /dev/null +++ b/challenge-269/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 269 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/ +# +# Task 1 - Bitwise OR +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 1, 2, 3, 4, 5 ], + [ 2, 3, 8, 16 ], + [ 1, 2, 5, 7, 9 ], +); + +sub bitwise_or { + my $nums = shift; + my $length = scalar @$nums; + + for (my $i = 0; $i < $length - 1; $i++) { + for (my $j = $i + 1; $j < $length; $j++) { + my $k = $nums->[$i] | $nums->[$j]; + + return 'true' if (($k % 2) == 0); + } + } + + return 'false'; +} + +for my $elements (@examples) { + my $bo = bitwise_or $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $bo; + say ' '; +} diff --git a/challenge-269/nelo-tovar/perl/ch-2.pl b/challenge-269/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..c4fc87269d --- /dev/null +++ b/challenge-269/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 269 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-269/ +# +# Task 2 - Distribute Elements +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 2, 1, 3, 4, 5 ], + [ 3, 2, 4], + [ 5, 4, 3 ,8], +); + +sub distribute_elements { + my $nums = shift; + my @ints = @$nums; + my @arr1 = (); + my @arr2 = (); + + push @arr1, shift @ints; + push @arr2, shift @ints; + + foreach my $n (@ints) { + if ($arr1[-1] > $arr2[-1]) { + push @arr1, $n + }else{ + push @arr2, $n + } + } + + return [@arr1,@arr2]; +} + +for my $elements (@examples) { + my $de = distribute_elements $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', dump(@$de); + say ' '; +} |
