diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-04 09:12:26 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-04 09:12:26 +0100 |
| commit | a70f67d573a19bb81d9f61ac53417a1211afd2ab (patch) | |
| tree | 2f5557602348522e34fc062f5e3c76f9c6a18705 | |
| parent | 2283c782a93d25451999b86435ac19ad29d46d24 (diff) | |
| parent | 29b24e697f45ac12ca1bc33e5fb84f684aed3851 (diff) | |
| download | perlweeklychallenge-club-a70f67d573a19bb81d9f61ac53417a1211afd2ab.tar.gz perlweeklychallenge-club-a70f67d573a19bb81d9f61ac53417a1211afd2ab.tar.bz2 perlweeklychallenge-club-a70f67d573a19bb81d9f61ac53417a1211afd2ab.zip | |
Merge pull request #10034 from ntovar/branch-267
Challenge 267. Add Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-267/nelo-tovar/bash/ch-1.sh | 36 | ||||
| -rwxr-xr-x | challenge-267/nelo-tovar/bash/ch-2.sh | 55 | ||||
| -rw-r--r-- | challenge-267/nelo-tovar/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-267/nelo-tovar/perl/ch-2.pl | 56 |
4 files changed, 183 insertions, 0 deletions
diff --git a/challenge-267/nelo-tovar/bash/ch-1.sh b/challenge-267/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..f59186b9e3 --- /dev/null +++ b/challenge-267/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 267 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/ +# +# Task 1 : Product Sign + +function product_sing() { + local nums=("$@") + local product=1 + local sign=0 + + for i in ${!nums[@]}; do + ((product*=${nums[$i]})) + done + + if [ $product -gt 0 ]; then + sign=1 + elif [ $product -lt 0 ]; then + sign=-1 + fi + + echo $sign +} + +examples=('-1 -2 -3 -4 3 2 1' '1 2 0 -2 -1' '-1 -1 1 -1 2') + +for e in ${!examples[@]}; do + array=(${examples[$e]}) + ps=($(product_sing "${array[@]}")) + echo "Input : ints = (${array[@]})" + echo "Output : $ps" + echo "" +done + diff --git a/challenge-267/nelo-tovar/bash/ch-2.sh b/challenge-267/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..a9adc3ebec --- /dev/null +++ b/challenge-267/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 267 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/ +# +# Task 2 : Line Counts + +function ord() { + printf "%d" "'$1" +} + +function line_count() { + local line=$1 + shift + local width=("$@") + local len=${#line} + local width_sum=0 + local line_counter=0 + local ord_a=$(ord 'a') + + for (( i = 0; i < $len; i++ )); do + ((letter=$(ord ${line:$i:1}) - $ord_a)) + ((b=$width_sum + ${width[$letter]})) + + if [ $b -le 100 ]; then + ((width_sum+=${width[$letter]})) + else + ((line_counter++)) + width_sum=${width[$letter]} + fi + done + + if [ $width_sum -gt 0 ]; then + ((line_counter++)) + fi + + + echo "$line_counter, $width_sum" +} + +example_lines=("abcdefghijklmnopqrstuvwxyz" "bbbcccdddaaa") +example_widths=( + '10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10' + '4 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10') + +for e in ${!example_lines[@]}; do + line=${example_lines[$e]} + width=(${example_widths[$e]}) + lc=$(line_count "$line" "${width[@]}") + echo "Input : line = $line" + echo "Input : width = ${width[@]}" + echo -e "Output : $lc\n" +done + diff --git a/challenge-267/nelo-tovar/perl/ch-1.pl b/challenge-267/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..78b72e107d --- /dev/null +++ b/challenge-267/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 267 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/ +# +# Task 1 - Product Sign +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ -1, -2, -3, -4, 3, 2, 1 ], + [ 1, 2, 0, -2, -1 ], + [ -1, -1, 1, -1, 2 ], +); + +sub product_sign { + my $nums = shift; + my $product = 1; + + $product *= $_ foreach (@$nums); + + return $product <=> 0; +} + +for my $elements (@examples) { + my $ps = product_sign $elements; + + say 'Input : @inst = ', dump(@$elements); + say 'Output : ', $ps; + say ' '; +} diff --git a/challenge-267/nelo-tovar/perl/ch-2.pl b/challenge-267/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..cd98b0a291 --- /dev/null +++ b/challenge-267/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 267 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/ +# +# Task 2 - Line Counts +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @example_lines = ("abcdefghijklmnopqrstuvwxyz", "bbbcccdddaaa"); + +my @example_widths =( + [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], + [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10], + ); + +sub line_counts { + my $line = shift; + my $widths = shift; + my $len = length $line; + my $width_sum = 0; + my $line_counter = 0; + my $ord_a = ord('a'); + + for (my $i = 0; $i < $len; $i++) { + my $letter = ord(substr($line,$i,1)) - $ord_a; + + if ($width_sum + @$widths[$letter] <= 100) { + $width_sum += @$widths[$letter] + }else { + $line_counter++; + $width_sum = @$widths[$letter]; + } + } + + $line_counter++ if ($width_sum); + + return [$line_counter, $width_sum]; +} + +for (my $i = 0; $i < scalar @example_lines; $i++) { + my $line = $example_lines[$i]; + my $width = $example_widths[$i]; + my $lc = line_counts $line, $width; + + $Data::Dump::LINEWIDTH = 100; + say 'Input : @str = ', $line; + say ' @widths = ', dump(@$width); + say 'Output : ', dump(@$lc); + say ' '; +} |
