diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-26 16:51:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-26 16:51:52 +0000 |
| commit | 359de5c4a127d8eb603ba5ba2757eb4e95af8440 (patch) | |
| tree | f7c804105cc2d58146d79c31445ec4a170437436 | |
| parent | c0b19ef00d3ef190e92407a8f2b211ad747d734f (diff) | |
| parent | e255c86f4c1b4ec52ad9b0fd09f464d16086e156 (diff) | |
| download | perlweeklychallenge-club-359de5c4a127d8eb603ba5ba2757eb4e95af8440.tar.gz perlweeklychallenge-club-359de5c4a127d8eb603ba5ba2757eb4e95af8440.tar.bz2 perlweeklychallenge-club-359de5c4a127d8eb603ba5ba2757eb4e95af8440.zip | |
Merge pull request #9464 from ntovar/branch-253
Challenge 253. Add Perl and Bash (ch-1) solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-253/nelo-tovar/bash/ch-1.sh | 40 | ||||
| -rw-r--r-- | challenge-253/nelo-tovar/perl/ch-1.pl | 39 | ||||
| -rw-r--r-- | challenge-253/nelo-tovar/perl/ch-2.pl | 57 |
3 files changed, 136 insertions, 0 deletions
diff --git a/challenge-253/nelo-tovar/bash/ch-1.sh b/challenge-253/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..34e41f3404 --- /dev/null +++ b/challenge-253/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 253 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/ +# +# Task 1 : Split Strings + +function split_strings() { + local separator=${!1} + local words=(${!2}) + local split=() + + IFS=$separator + for i in ${words[@]}; do + if [[ -n "$i" ]]; then + split+=($i) + fi + done + + echo ${split[@]} +} + + +example1='one.two.three four.five six' +separator1='.' +example2='$perl$$ $$raku$' +separator2='$' + +for e in 1 2; do + v_w="example$e" + v_s="separator$e" + + ss=($(split_strings $v_s $v_w)) + echo "Input : words = (${!v_w})" + echo " separator = ${!v_s}" + echo "Output : (${ss[@]})" + echo "" +done + diff --git a/challenge-253/nelo-tovar/perl/ch-1.pl b/challenge-253/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..770fa72740 --- /dev/null +++ b/challenge-253/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 253 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/ +# +# Task 1 - Split Strings +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples_words = ( + [ "one.two.three","four.five","six" ], + [ '$perl$$', '$$raku$' ], +); +my @separators = ('.', '$'); + +sub split_string { + my $separator = quotemeta(shift); + my $words = shift; + my @split; + + push(@split, grep {length $_} map({split(/$separator/, $_)} @$words)); + + return \@split; +} + +for my $elements (@examples_words) { + my $separator = shift @separators; + my $ss = split_string($separator, $elements); + + say 'Input : @words = ', dump(@$elements); + say ' $separator = ', $separator; + say 'Output : ', dump(@$ss); + say ' '; +} diff --git a/challenge-253/nelo-tovar/perl/ch-2.pl b/challenge-253/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..a958b88779 --- /dev/null +++ b/challenge-253/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 253 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-253/ +# +# Task 2 - Weakest Row +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); +use List::Util qw/sum0/; + +my @examples = ( + [ + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 0], + [1, 0, 0, 0, 0], + [1, 1, 0, 0, 0], + [1, 1, 1, 1, 1] + ], + [ + [1, 0, 0, 0], + [1, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 0] + ] +); + +sub weakest_row { + my $matrix = shift; + my $cols = scalar @$matrix; + my @rows = (); + my %sum_rows; + + for (my $i = 0; $i < $cols; $i++) { + $sum_rows{$i} = sum0 @{$matrix->[$i]}; + } + + @rows = sort { $sum_rows{$a} <=> $sum_rows{$b} } sort keys %sum_rows; + + return \@rows; +} + +for my $elements (@examples) { + my $wr = weakest_row $elements; + + say 'Input : $matrix = ['; + foreach my $x (@$elements) { + printf("%19s%s,\n", ' ', dump($x)) + }; + printf("%18s\n", ']'); + say 'Output : ', dump($wr); + say ' '; +} |
