diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-19 22:02:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-19 22:02:37 +0100 |
| commit | e7f6bf55c03a979dac64496dd22f88fcb85bdfce (patch) | |
| tree | 8ecf6a98f4a12ab0d5692f073fce3454bcc78402 | |
| parent | e8feffec5ae496626ef43d772ea8c956119bf534 (diff) | |
| parent | 76459ffc75678536150691f8d689af9728664ea0 (diff) | |
| download | perlweeklychallenge-club-e7f6bf55c03a979dac64496dd22f88fcb85bdfce.tar.gz perlweeklychallenge-club-e7f6bf55c03a979dac64496dd22f88fcb85bdfce.tar.bz2 perlweeklychallenge-club-e7f6bf55c03a979dac64496dd22f88fcb85bdfce.zip | |
Merge pull request #9956 from ntovar/branch-265
Challenge 265. Add Perl and Bash (ch-1.sh) solutions. By Nelo Tovar.
| -rwxr-xr-x | challenge-265/nelo-tovar/bash/ch-1.sh | 40 | ||||
| -rw-r--r-- | challenge-265/nelo-tovar/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-265/nelo-tovar/perl/ch-2.pl | 58 |
3 files changed, 144 insertions, 0 deletions
diff --git a/challenge-265/nelo-tovar/bash/ch-1.sh b/challenge-265/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..ce24e70c6d --- /dev/null +++ b/challenge-265/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 265 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-265/ +# +# Task 1 : 33% Appearance + +function _33_percent_appearence() { + local nums=("$@") + local length=${#nums[@]} + local percent=$((($length * 33 + 100 -1) / 100)) + local frequencies + local candidates + + for i in "${nums[@]}"; do + ((frequencies[$i]++)) + done + + for key in ${!frequencies[@]}; do + if [[ ${frequencies[$key]} -ge $percent ]]; then + candidates+=($key) + fi + done + + candidates=($(IFS=$'\n'; echo "${candidates[*]}" | sort -n)) + + echo ${candidates[0]} +} + +examples=('1 2 3 3 3 3 4 2' '1 1' '1 2 3') + +for e in ${!examples[@]}; do + array=(${examples[$e]}) + pa=$(_33_percent_appearence "${array[@]}") + echo "Input : inst = (${array[@]})" + echo "Output : $pa" + echo "" +done + diff --git a/challenge-265/nelo-tovar/perl/ch-1.pl b/challenge-265/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..7edb5f137f --- /dev/null +++ b/challenge-265/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 265 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-265/ +# +# Task 1 - 33% Appearance +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); +use POSIX qw(ceil); + +my @examples = ( + [ 1, 2, 3, 3, 3, 3, 4, 2 ], + [ 1, 1 ], + [ 1, 2, 3 ], +); + +sub _33_percent_appearance { + my $nums = shift; + my $len = scalar @$nums; + my $percent = ceil($len * 0.33); + my %frequencies; + my @candidates; + + $frequencies{$_}++ for (@$nums); + + foreach my $k (keys %frequencies) { + push @candidates,$k if ($frequencies{$k} >= $percent ); + } + + + return (sort {$a<=> $b} @candidates)[0]; + +} + +for my $elements (@examples) { + my $pa = _33_percent_appearance $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $pa; + say ' '; +} diff --git a/challenge-265/nelo-tovar/perl/ch-2.pl b/challenge-265/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..2179fbca33 --- /dev/null +++ b/challenge-265/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 265 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-265/ +# +# Task 2 - Completing Word +# + +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 = ( + { str => 'aBc 11c', words => ['accbbb', 'abc', 'abbc'] }, + { str => 'Da2 abc', words => ['abcm', 'baacd', 'abaadc'] }, + { str => 'JB 007', words => ['jj', 'bb', 'bjb'] }, +); + +sub completing_word { + my $string = lc(shift); + my $words = shift; + my %frequencies; + my @candidates = (); + + foreach my $letter (split //, $string) { + $frequencies{$letter}++ if ($letter =~ /[a-zA-Z]/) + } + + foreach my $word ($words->@*) { + $word = lc($word); + my $is_candidate = 1; + foreach my $k (keys %frequencies) { + my @letters = $word =~ m/[$k]/g; + + $is_candidate = 0 if ($frequencies{$k} > scalar @letters); + + last if (!$is_candidate); + } + push @candidates, $word if $is_candidate; + } + + return (sort({length($a) <=> length($b)} @candidates))[0]; +} + +for my $elements (@examples) { + my $string = $elements->{str}; + my $words = $elements->{words}; + my $cw = completing_word $string, $words; + + say "Input : \$str = '$string'"; + say ' : @str = ', dump(@$words); + say 'Output : ', $cw; + say ' '; +} |
