diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-10 09:41:22 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-10 09:41:22 +0000 |
| commit | 86ca603c8f0aff77eb5bfb5721b603bb1f5df538 (patch) | |
| tree | ca426bf56ebfd36c6bab162280078b0313202af2 | |
| parent | 2815022118491954bd188e43c3611fe71acf1e75 (diff) | |
| parent | a5c1feacd6cbfca6d3c2468ad0885f75f1a75d6f (diff) | |
| download | perlweeklychallenge-club-86ca603c8f0aff77eb5bfb5721b603bb1f5df538.tar.gz perlweeklychallenge-club-86ca603c8f0aff77eb5bfb5721b603bb1f5df538.tar.bz2 perlweeklychallenge-club-86ca603c8f0aff77eb5bfb5721b603bb1f5df538.zip | |
Merge pull request #9551 from ntovar/branch-255
Challenge 255. Add Perl and Bash solutions. By Nelo Tovar.
| -rw-r--r-- | challenge-255/nelo-tovar/bash/2 | 1 | ||||
| -rwxr-xr-x | challenge-255/nelo-tovar/bash/ch-1.sh | 45 | ||||
| -rwxr-xr-x | challenge-255/nelo-tovar/bash/ch-2.sh | 48 | ||||
| -rw-r--r-- | challenge-255/nelo-tovar/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-255/nelo-tovar/perl/ch-2.pl | 46 |
5 files changed, 182 insertions, 0 deletions
diff --git a/challenge-255/nelo-tovar/bash/2 b/challenge-255/nelo-tovar/bash/2 new file mode 100644 index 0000000000..c7c3a5dc0b --- /dev/null +++ b/challenge-255/nelo-tovar/bash/2 @@ -0,0 +1 @@ +word = l diff --git a/challenge-255/nelo-tovar/bash/ch-1.sh b/challenge-255/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..4852cebebb --- /dev/null +++ b/challenge-255/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 255 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +# +# Task 1 : Odd Character + +function odd_character() { + local s=$1 + local t=$2 + declare -A frequency=() + + for (( i = 0; i < ${#t}; i++ )); do + letter=${t:$i:1} + ((frequency[$letter]++)) + done + + for (( i = 0; i < ${#s}; i++ )); do + letter=${s:$i:1} + ((frequency[$letter]--)) + done + + for key in "${!frequency[@]}"; do + if [ ${frequency[$key]} -gt 0 ]; then + echo $key + return + fi + done + + echo ' ' +} + + +declare -A examples=([Perl]=Preel [Weekly]=Weealy [Box]=Boxy) + +for key in "${!examples[@]}"; do + s=$key + t=${examples[$key]} + oc=$(odd_character $s $t) + echo "Input : s = $s t = $t" + echo "Output : $oc" + echo "" +done + diff --git a/challenge-255/nelo-tovar/bash/ch-2.sh b/challenge-255/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..625d547947 --- /dev/null +++ b/challenge-255/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 255 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +# +# Task 2 : Most Frequent Word + +function most_frequent_word() { + local p=($1) + local w=$2 + declare -A most_frequent=([max]=0 [word]='') + declare -A frequency=() + + for i in ${p[@]}; do + [[ $i =~ ([a-zA-Z]+).* ]] + word=${BASH_REMATCH[1]} + + if [[ "$word" != "$w" ]]; then + ((frequency[$word]++)) + fi + done + + for key in "${!frequency[@]}"; do + if [ ${frequency[$key]} -gt ${most_frequent[max]} ]; then + most_frequent[max]=${frequency[$key]} + most_frequent[word]=$key + fi + done + + echo ${most_frequent[word]} + +} + +example_p=('Joe hit a ball, the hit ball flew far after it was hit.' + 'Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.') +example_w=('hit' 'the') + +for i in 0 1; do + p=${example_p[$i]} + w=${example_w[$i]} + + mfw=$(most_frequent_word "$p" "$w") + echo "Input : p = $p" + echo " w = $w" + echo -e "Output : $mfw\n" +done + diff --git a/challenge-255/nelo-tovar/perl/ch-1.pl b/challenge-255/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..fd81d9ea48 --- /dev/null +++ b/challenge-255/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 255 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +# +# Task 1 - Odd Character +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + {s => 'Perl', t => 'Preel'}, + {s => 'Weekly', t => 'Weeakly'}, + {s => 'Box', t => 'Boxy'} +); + +sub odd_character { + my ($s, $t) = @_; + my %frequency; + + $frequency{$_}++ foreach (split //, $t); + $frequency{$_}-- foreach (split //, $s); + + foreach my $k (keys %frequency) { + + return $k if $frequency{$k} > 0; + } + + return ' '; +} + +for my $elements (@examples) { + my $oc = odd_character $elements->{s}, $elements->{t}; + + printf "Input : \$s = \"%s\" \$t = \"%s\"\n", $elements->{s}, $elements->{t}; + say 'Output : ', $oc; + say ' '; +} diff --git a/challenge-255/nelo-tovar/perl/ch-2.pl b/challenge-255/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..1e7edfe267 --- /dev/null +++ b/challenge-255/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 255 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-255/ +# +# Task 2 - Most Frequent Word +# + +use strict; +use warnings; +use v5.28; +use List::MoreUtils qw(frequency); +use Data::Dump qw(dump); + +my @examples = ( + {p => 'Joe hit a ball, the hit ball flew far after it was hit.', + w => 'hit'}, + {p => 'Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.', + w => 'the'}, +); + +sub most_frequent_word { + my ($p, $w) = @_; + my @words = grep {$_ ne $w} split /\W/, $p; + my %most_frequent = (max => 0, word => '') ; + my %frequency = frequency @words; + + foreach my $k (keys %frequency) { + if ($frequency{$k} > $most_frequent{max}){ + $most_frequent{word} = $k; + $most_frequent{max} = $frequency{$k}; + } + } + + return $most_frequent{word}; +} + +for my $elements (@examples) { + my $mfw = most_frequent_word $elements->{p}, $elements->{w}; + + say 'Input : $p = ', $elements->{p}; + say ' $w = ', $elements->{w}; + say 'Output : ', $mfw; + say ' '; +} |
