diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-24 22:09:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-24 22:09:19 +0000 |
| commit | c05edc10016fcc6769ef2cd764ff05f6d1f17181 (patch) | |
| tree | a94e6679f9fcdd665a87eea7ce73caf46dcaf221 | |
| parent | 1898944f5b01a9d2ebc83dba32f333762688e69b (diff) | |
| parent | e4feb01c9efd5e9b8bef29bb02a18cd53a3e56b0 (diff) | |
| download | perlweeklychallenge-club-c05edc10016fcc6769ef2cd764ff05f6d1f17181.tar.gz perlweeklychallenge-club-c05edc10016fcc6769ef2cd764ff05f6d1f17181.tar.bz2 perlweeklychallenge-club-c05edc10016fcc6769ef2cd764ff05f6d1f17181.zip | |
Merge pull request #9124 from ntovar/branch-244
Challenge 244. Add Perl and Bash solutions. By Nelo Tovar.
| -rwxr-xr-x | challenge-244/nelo-tovar/bash/c.sh | 16 | ||||
| -rwxr-xr-x | challenge-244/nelo-tovar/bash/ch-1.sh | 38 | ||||
| -rwxr-xr-x | challenge-244/nelo-tovar/bash/ch-2.sh | 74 | ||||
| -rw-r--r-- | challenge-244/nelo-tovar/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-244/nelo-tovar/perl/ch-2.pl | 43 |
5 files changed, 213 insertions, 0 deletions
diff --git a/challenge-244/nelo-tovar/bash/c.sh b/challenge-244/nelo-tovar/bash/c.sh new file mode 100755 index 0000000000..9177390259 --- /dev/null +++ b/challenge-244/nelo-tovar/bash/c.sh @@ -0,0 +1,16 @@ +list=(2 1 4) + +combinations() { + local -a results=() + let idx=$2 + for (( j = 0; j < $1; j++ )); do + if (( idx % 2 )); then results=("${results[@]}" "${list[$j]}"); fi + let idx\>\>=1 + done + echo "${results[@]}" +} + +n=${#list[@]} +for (( i = 1; i < 2**n; i++ )); do + combinations $n $i +done diff --git a/challenge-244/nelo-tovar/bash/ch-1.sh b/challenge-244/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..cd10c78f57 --- /dev/null +++ b/challenge-244/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 244 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/ +# +# Task 1 : Count Smaller + +function count_smaller() { + local nums=("$@") + local length=${#nums[@]} + local smallers=() + + for (( i = 0; i < $length; i++ )); do + count=0 + for (( j = 0 ; j < $length; j++ )); do + if [[ ${nums[$j]} -lt ${nums[$i]} ]]; then + ((count++)) + fi + done + smallers+=($count) + done + + echo ${smallers[@]} +} + +example1='8 1 2 2 3' +example2='6 5 4 8' +example3='2 2 2' + +for e in "$example1" "$example2" "$example3"; do + array=($e) + cs=($(count_smaller "${array[@]}")) + echo "Input : nums = (${array[@]})" + echo "Output : (${cs[@]})" + echo "" +done + diff --git a/challenge-244/nelo-tovar/bash/ch-2.sh b/challenge-244/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..451c6687c5 --- /dev/null +++ b/challenge-244/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 244 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/ +# +# Task 2 : Group Hero + +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 group_hero() { + local nums=("$@") + local n=${#nums[@]} + local sum=0 + + for (( k = 1; k < 2**n; k++ )); do + c=($(combinations $k "${array[@]}")) + min=$(min "${c[@]}") + max=$(max "${c[@]}") + ((sum+=($max ** 2) * $min)) + done + + echo $sum +} + +example1='2 1 4' + +for e in "$example1"; do + array=($e) + gh=$(group_hero "${array[@]}") + echo "Input : nums = (${array[@]})" + echo -e "Output : $gh\n" +done + diff --git a/challenge-244/nelo-tovar/perl/ch-1.pl b/challenge-244/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..f94c2933ea --- /dev/null +++ b/challenge-244/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 244 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/ +# +# Task 1 - Count Smaller +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 8, 1, 2, 2, 3 ], + [ 6, 5, 4, 8 ], + [ 2, 2, 2 ], +); + +sub count_smaller { + my $nums = shift; + my @smallers; + + foreach my $x (@$nums) { + my $count = 0; + foreach my $y (@$nums){ + $count++ if ($y < $x); + } + push(@smallers, $count) + } + + return \@smallers; +} + +for my $elements (@examples) { + my $cs = count_smaller $elements; + + say 'Input : @nums = ', dump(@$elements); + say 'Output : ', dump(@$cs); + say ' '; +} diff --git a/challenge-244/nelo-tovar/perl/ch-2.pl b/challenge-244/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..f548735a36 --- /dev/null +++ b/challenge-244/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 244 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-244/ +# +# Task 2 - Group Hero +# + +use strict; +use warnings; +use v5.28; +use List::Util qw (min max); +use Algorithm::Combinatorics qw(combinations); +use Data::Dump qw(dump); + +my @examples = ( + [ 2, 1, 4 ], +); + +sub group_hero { + my $nums = shift; + my $length = scalar @$nums; + my $sum = 0; + + for (my $k = 1; $k <= $length; $k++) { + my $iter = combinations($nums, $k); + + while (my $c = $iter->next) { + $sum += (max(@$c) ** 2) * min(@$c) + } + } + + return $sum; +} + +for my $elements (@examples) { + my $gh = group_hero $elements; + + say 'Input : @nums = ', dump(@$elements); + say 'Output : ', $gh; + say ' '; +} |
