diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-27 23:10:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-27 23:10:18 +0100 |
| commit | d02f0ea48f8dde6acc68801362d1156abaeea3b8 (patch) | |
| tree | f578a3de6d8397acd66d3c807a5bd5dc6fd187f4 | |
| parent | b21e67f10b5b6a2396541792e98a339e033469bf (diff) | |
| parent | e36ffdb35e95cbd2718f58940393db73c0541614 (diff) | |
| download | perlweeklychallenge-club-d02f0ea48f8dde6acc68801362d1156abaeea3b8.tar.gz perlweeklychallenge-club-d02f0ea48f8dde6acc68801362d1156abaeea3b8.tar.bz2 perlweeklychallenge-club-d02f0ea48f8dde6acc68801362d1156abaeea3b8.zip | |
Merge pull request #10497 from ntovar/branch-279
Challenge 279. Add Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-279/nelo-tovar/bash/ch-1.sh | 36 | ||||
| -rwxr-xr-x | challenge-279/nelo-tovar/bash/ch-2.sh | 30 | ||||
| -rw-r--r-- | challenge-279/nelo-tovar/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-279/nelo-tovar/perl/ch-2.pl | 33 |
4 files changed, 146 insertions, 0 deletions
diff --git a/challenge-279/nelo-tovar/bash/ch-1.sh b/challenge-279/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..100c5f4463 --- /dev/null +++ b/challenge-279/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 279 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/ +# +# Task 1 - Sort Letters + +function sort_letters() { + local letters=($1) + local weights=($2) + local word='' + declare -A temp + + for i in "${!letters[@]}"; do + temp[${weights[$i]}]=${letters[$i]} + done + + for key in $(echo ${weights[@]} | tr " " "\n" | sort -n); do + word+=${temp[$key]} + done + + echo $word +} + +example_letters=('R E P L' 'A U R K' 'O H Y N P T') +example_weights=('3 2 1 4' '2 4 1 3' '5 4 2 6 1 3') + +for e in ${!example_letters[@]}; do + sl=($(sort_letters "${example_letters[$e]}" "${example_weights[$e]}")) + echo "Input : letters = (${example_letters[$e]})" + echo " weights = (${example_weights[$e]})" + echo "Output : $sl" + echo "" +done + diff --git a/challenge-279/nelo-tovar/bash/ch-2.sh b/challenge-279/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..9787f7abd7 --- /dev/null +++ b/challenge-279/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 279 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/ +# +# Task 2 - Split String + +function split_string() { + local word=$1 + word=${word,} + local consonants=$(echo $word | tr -d [aeiou]) + ((vowels_count=${#word} - ${#consonants})) + + if [ $(($vowels_count % 2)) -eq 0 ]; then + echo true + else + echo false + fi + +} + +examples=('perl' 'book' 'good morning') + +for e in "${examples[@]}"; do + ss=$(split_string "$e") + echo "Input : str = ($e)" + echo -e "Output : $ss\n" +done + diff --git a/challenge-279/nelo-tovar/perl/ch-1.pl b/challenge-279/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..62800a3bad --- /dev/null +++ b/challenge-279/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 279 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/ +# +# Task 1 - Sort Letters +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + {letters => ['R', 'E', 'P', 'L'], weights => [3, 2, 1, 4]}, + {letters => ['A', 'U', 'R', 'K'], weights => [2, 4, 1, 3]}, + {letters => ['O', 'H', 'Y', 'N', 'P', 'T'], weights => [5, 4, 2, 6, 1, 3]}, + +); + +sub sort_letters { + my $elements = shift; + my @letters = $elements->{letters}->@*; + my $length = scalar @letters; + my @weights = $elements->{weights}->@*; + my %temp; + my $word; + + for (my $i = 0; $i < $length; $i++) { + $temp{$weights[$i]} = [$i,$letters[$i]] + } + + $word .= $letters[$temp{$_}[0]] foreach (sort keys %temp); + + return $word + +} + +for my $elements (@examples) { + my $sl = sort_letters $elements; + + say 'Input : @letters = ', dump(@$elements{letters}); + say 'Input : @weights = ', dump(@$elements{weights}); + say 'Output : ', $sl; + say ' '; +} diff --git a/challenge-279/nelo-tovar/perl/ch-2.pl b/challenge-279/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..e8c5dd2e4f --- /dev/null +++ b/challenge-279/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 279 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/ +# +# Task 2 - Split String +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ('perl', 'book', 'good morning'); + +sub split_string { + my $word = shift; + $_ = lc $word; + my $count = tr/aeiou//; + my $return = 'false'; + + return $count % 2 == 0 ? 'true': 'false'; + +} + +for my $elements (@examples) { + my $ss = split_string $elements; + + say 'Input : str = ', $elements; + say 'Output : ', $ss; + say ' '; +} |
