diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-16 22:15:12 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-16 22:15:12 +0100 |
| commit | 358805e9bcea836557c3d6052c04ef118d4ec393 (patch) | |
| tree | 015bfb0d09f78bb870e1f118c0ffe9303f36db37 | |
| parent | 5c202b06e431bb4d4da8286d9e6639a887c6cc95 (diff) | |
| parent | 2f0102bdd9f9803b56887fe4487ca46e9ca9144b (diff) | |
| download | perlweeklychallenge-club-358805e9bcea836557c3d6052c04ef118d4ec393.tar.gz perlweeklychallenge-club-358805e9bcea836557c3d6052c04ef118d4ec393.tar.bz2 perlweeklychallenge-club-358805e9bcea836557c3d6052c04ef118d4ec393.zip | |
Merge pull request #10276 from ntovar/branch-273
Challenge 273. Add Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-273/nelo-tovar/bash/ch-1.sh | 32 | ||||
| -rwxr-xr-x | challenge-273/nelo-tovar/bash/ch-2.sh | 26 | ||||
| -rw-r--r-- | challenge-273/nelo-tovar/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-273/nelo-tovar/perl/ch-2.pl | 28 |
4 files changed, 124 insertions, 0 deletions
diff --git a/challenge-273/nelo-tovar/bash/ch-1.sh b/challenge-273/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..ecae9d02ff --- /dev/null +++ b/challenge-273/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 273 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +# +# Task 1 : Percentage of Character + +function percentage_of_character() { + local str=$1 + local char=$2 + local length=${#str} + local without_char=${str//$char/} + local len_without_char=${#without_char} + ((count=length-len_without_char)) + percentage=$(echo "scale=2; 0.05+100*$count/$length" | bc ) + + printf '%.0f' $percentage +} + +examples_str=('perl' 'java' 'python' 'ada' 'ballerina' 'analitik' ) +examples_char=('e' 'a' 'm' 'a' 'l' 'k') + +for e in ${!examples_str[@]}; do + str=${examples_str[$e]} + char=${examples_char[$e]} + pof=$(percentage_of_character $str $char) + echo "Input : str = $str, chr = $char" + echo "Output : $pof" + echo "" +done + diff --git a/challenge-273/nelo-tovar/bash/ch-2.sh b/challenge-273/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..57cd0a938a --- /dev/null +++ b/challenge-273/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 273 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +# +# Task 2 : B After A + +function b_after_a() { + local str=$1 + + if [[ "$str" =~ bb|^b+$ ]]; then + echo 'true' + else + echo 'false' + fi +} + +examples=('aabb' 'abab' 'aaa' 'bbb') + +for e in ${examples[@]}; do + baa=$(b_after_a "$e") + echo "Input : str = $e" + echo -e "Output : $baa\n" +done + diff --git a/challenge-273/nelo-tovar/perl/ch-1.pl b/challenge-273/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..74fdda38b8 --- /dev/null +++ b/challenge-273/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 273 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +# +# Task 1 - Percentage of Character +# + +use strict; +use warnings; +use v5.28; +use POSIX qw/ceil/; + +my @examples = ( + {str => 'perl', chr => 'e' }, + {str => 'java', chr => 'a' }, + {str => 'python', chr => 'm' }, + {str => 'ada', chr => 'a' }, + {str => 'ballerina', chr => 'l' }, + {str => 'analitik', chr => 'k' }, +); + +sub percentage_of_character { + my $element = shift; + my $str_len = length($element->{str}); + my $count = () = $element->{str} =~ /$element->{chr}/g; + + return int(($count * 100 / $str_len) + 0.5) + +} + +for my $elements (@examples) { + my $poc = percentage_of_character $elements; + + printf "Input : str = '%s', chr = '%s'\n", $elements->{str}, $elements->{chr}; + printf "Output : %s\n\n", $poc; +} diff --git a/challenge-273/nelo-tovar/perl/ch-2.pl b/challenge-273/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..a4a91aa546 --- /dev/null +++ b/challenge-273/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 273 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/ +# +# Task 2 - B After A +# + +use strict; +use warnings; +use v5.28; + +my @examples = ( 'aabb', 'abab', 'aaa', 'bbb'); + +sub b_after_a { + my $str = shift; + + return ( $str =~ /bb|^b+$/ ? 'true' : 'false'); +} + +for my $element (@examples) { + my $baa = b_after_a $element; + + printf "Input : str = '%s'\n", $element; + printf "Output : %s\n", $baa; + say ' '; +} |
