diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-05 21:03:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-05 21:03:08 +0000 |
| commit | e8fd698aefda63f42ea5379c0d67f953a2a1b1b9 (patch) | |
| tree | c876bb06ac3e831a70ea022125fe8847d56db4b9 | |
| parent | 1cb7055f98e56e547e8ffa18fd412350e2cba5bd (diff) | |
| parent | 305a25a2a4133b582d4f7c3bec050d86403889f0 (diff) | |
| download | perlweeklychallenge-club-e8fd698aefda63f42ea5379c0d67f953a2a1b1b9.tar.gz perlweeklychallenge-club-e8fd698aefda63f42ea5379c0d67f953a2a1b1b9.tar.bz2 perlweeklychallenge-club-e8fd698aefda63f42ea5379c0d67f953a2a1b1b9.zip | |
Merge pull request #9531 from PerlBoy1967/branch-for-challenge-255
w255 - Task 1 & 2
| -rwxr-xr-x | challenge-255/perlboy1967/perl/ch1.pl | 38 | ||||
| -rwxr-xr-x | challenge-255/perlboy1967/perl/ch2.pl | 38 |
2 files changed, 76 insertions, 0 deletions
diff --git a/challenge-255/perlboy1967/perl/ch1.pl b/challenge-255/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..e2946127cb --- /dev/null +++ b/challenge-255/perlboy1967/perl/ch1.pl @@ -0,0 +1,38 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 255 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-255 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Odd Character +Submitted by: Mohammad Sajid Anwar + +You are given two strings, $s and $t. The string $t is generated using the shuffled +characters of the string $s with an additional character. + +Write a script to find the additional character in the string $t. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::MoreUtils qw(firstidx); + +sub oddCharacter ($s,$t) { + my @s = sort split //, $s; + my @t = sort split //, $t; + return $t[firstidx { $s[$_] ne $t[$_] } 0 .. $#s]; +} + +is(oddCharacter('Perl','Preel'),'e'); +is(oddCharacter('Weekly','Weeakly'),'a'); +is(oddCharacter('Box','Boxy'),'y'); + +done_testing; diff --git a/challenge-255/perlboy1967/perl/ch2.pl b/challenge-255/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..2dd8380bce --- /dev/null +++ b/challenge-255/perlboy1967/perl/ch2.pl @@ -0,0 +1,38 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 255 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-255 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Most Frequent Word +Submitted by: Mohammad Sajid Anwar + +You are given a paragraph $p and a banned word $w. + +Write a script to return the most frequent word that is not banned. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0; + +use List::Util qw(max); +use List::MoreUtils qw(frequency); + +sub almostFrequentWord ($p,$ban) { + my %f = frequency grep !/$ban/, split /\W+/, $p; + my %w; push(@{$w{$f{$_}}},$_) for (keys %f); + $w{max(keys %w)}; +} + +is(almostFrequentWord('five dot three dot three','dot'),['three']); +is(almostFrequentWord('one plus one plus one equals three','one'),['plus']); + +done_testing; + |
