diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-18 21:19:28 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-18 21:19:28 +0000 |
| commit | 0904f67389aa96bc5f57def2d70420b00b8ee495 (patch) | |
| tree | f11f7f09aa77cf6e8c58a7effd0acfdf06989f66 /challenge-252 | |
| parent | 0049e22c3e69933d44e3691c38e8a6824f931140 (diff) | |
| parent | 66c5364faab0a6fbcc294aad75e7428243168826 (diff) | |
| download | perlweeklychallenge-club-0904f67389aa96bc5f57def2d70420b00b8ee495.tar.gz perlweeklychallenge-club-0904f67389aa96bc5f57def2d70420b00b8ee495.tar.bz2 perlweeklychallenge-club-0904f67389aa96bc5f57def2d70420b00b8ee495.zip | |
Merge pull request #9423 from ntovar/branch-252
Challenge 252. Add Perl and Bash solutions. By Nelo Tovar
Diffstat (limited to 'challenge-252')
| -rwxr-xr-x | challenge-252/nelo-tovar/bash/ch-1.sh | 33 | ||||
| -rwxr-xr-x | challenge-252/nelo-tovar/bash/ch-2.sh | 33 | ||||
| -rw-r--r-- | challenge-252/nelo-tovar/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-252/nelo-tovar/perl/ch-2.pl | 38 |
4 files changed, 142 insertions, 0 deletions
diff --git a/challenge-252/nelo-tovar/bash/ch-1.sh b/challenge-252/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..df2ab7a9d1 --- /dev/null +++ b/challenge-252/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 252 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +# +# Task 1 : Special Numbers + +function special_numbers() { + local ints=("$@") + local n=${#ints[@]} + local sum=0 + + for x in ${ints[@]}; do + if [[ $(($n % $x)) -eq 0 ]]; then + ((sum+=$(($x ** 2)))) + fi + done + + echo $sum +} + +example1='1 2 3 4' +example2='2 7 1 19 18 3' + +for e in "$example1" "$example2" ; do + array=($e) + sn=$(special_numbers "${array[@]}") + echo "Input : @ints = (${array[@]})" + echo "Output : $sn" + echo "" +done + diff --git a/challenge-252/nelo-tovar/bash/ch-2.sh b/challenge-252/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..27749455b8 --- /dev/null +++ b/challenge-252/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,33 @@ +# +# The Weekly Challenge 252 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +# +# Task 2 : Unique Sum Zero + +function unique_sum_zero() { + local n=$1 + local numbers=() + + if [[ $(($n % 2)) -ne 0 || $n -eq 1 ]]; then + numbers=(0) + fi + + for (( k = 1; k <= $(($n / 2)); k++ )); do + i=$(shuf -i 1-10 -n 1) + numbers+=($i -$i) + done + + echo ${numbers[@]} +} + +example1='5 3 1' + +for e in $example1; do + array=($e) + usz=$(unique_sum_zero $e) + echo "Input : $s = $e" + echo "Output :" $(printf " %s, " ${usz[@]}) + echo " " +done + diff --git a/challenge-252/nelo-tovar/perl/ch-1.pl b/challenge-252/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..266e83ce97 --- /dev/null +++ b/challenge-252/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 252 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +# +# Task 1 - Special Numbers +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 1, 2, 3, 4 ], + [ 2, 7, 1, 19, 18, 3 ], +); + +sub special_numbers { + my $ints = shift; + my $n = scalar @$ints; + my $sum = 0; + + foreach my $x (@$ints) { + $sum += $x ** 2 unless ($n % $x); + } + + return $sum; +} + +for my $elements (@examples) { + my $sn = special_numbers $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $sn; + say ' '; +} diff --git a/challenge-252/nelo-tovar/perl/ch-2.pl b/challenge-252/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..ad6020cf78 --- /dev/null +++ b/challenge-252/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 252 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +# +# Task 2 - Unique Sum Zero +# + +use strict; +use warnings; +use v5.28; +use List::Util qw (min max); +use Data::Dump qw(dump); + +my @examples = ( 5, 3, 1 , ); + +sub unique_sum_zero { + my $n = shift; + my @numbers = (); + + push @numbers,0 if ($n % 2 != 0) or ($n == 1); + + for (my $k = 1; $k <= $n / 2; $k++) { + my $i = int rand(9) + 1; + push @numbers, ($i, -$i) + } + + return \@numbers; +} + +for my $elements (@examples) { + my $usz = unique_sum_zero $elements; + + say 'Input : $s = ', $elements; + say 'Output : ', dump($usz); + say ' '; +} |
