aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-05 19:12:25 +0000
committerGitHub <noreply@github.com>2024-02-05 19:12:25 +0000
commitba3dea3468800b07b6e59414fdde55e99607e7c5 (patch)
tree43e6f375e599f51bab1c52a14ac106b06725b175
parent3f5d5e30307e368e8ce61541cad4b940ea91b2b1 (diff)
parent1eb9d35930716d2dfa8f723e3ff60e0228137e20 (diff)
downloadperlweeklychallenge-club-ba3dea3468800b07b6e59414fdde55e99607e7c5.tar.gz
perlweeklychallenge-club-ba3dea3468800b07b6e59414fdde55e99607e7c5.tar.bz2
perlweeklychallenge-club-ba3dea3468800b07b6e59414fdde55e99607e7c5.zip
Merge pull request #9528 from pme/challenge-255
challenge-255
-rwxr-xr-xchallenge-255/peter-meszaros/perl/ch-1.pl48
-rwxr-xr-xchallenge-255/peter-meszaros/perl/ch-2.pl55
2 files changed, 103 insertions, 0 deletions
diff --git a/challenge-255/peter-meszaros/perl/ch-1.pl b/challenge-255/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..1e7be09a95
--- /dev/null
+++ b/challenge-255/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+#
+# 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..
+# Example 1
+#
+# Input: $s = "Perl" $t = "Preel"
+# Output: "r"
+#
+# Example 2
+#
+# Input: $s = "Weekly" $t = "Weeakly"
+# Output: "a"
+#
+# Example 3
+#
+# Input: $s = "Box" $t = "Boxy"
+# Output: "y"
+#
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ ['Perl', 'Preel'],
+ ['Weekly', 'Weeakly'],
+ ['Box', 'Boxy'],
+];
+
+sub odd_character
+{
+ my ($s, $t) = @_;
+
+ ($s ^ $t) =~ /[^\000]/;
+
+ return substr($t, $+[0]-1, 1);
+}
+
+is(odd_character($cases->[0]->@*), 'r', 'Example 1');
+is(odd_character($cases->[1]->@*), 'a', 'Example 2');
+is(odd_character($cases->[2]->@*), 'y', 'Example 3');
+done_testing();
+
+exit 0;
diff --git a/challenge-255/peter-meszaros/perl/ch-2.pl b/challenge-255/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..30f0c6ca46
--- /dev/null
+++ b/challenge-255/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl
+#
+# You are given a paragraph $p and a banned word $w.
+#
+# Write a script to return the most frequent word that is not banned.
+# Example 1
+#
+# Input: $p = "Joe hit a ball, the hit ball flew far after it was hit."
+# $w = "hit"
+# Output: "ball"
+#
+# The banned word "hit" occurs 3 times.
+# The other word "ball" occurs 2 times.
+#
+# Example 2
+#
+# Input: $p = "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge."
+# $w = "the"
+# Output: "Perl"
+#
+# The banned word "the" occurs 3 times.
+# The other word "Perl" occurs 2 times.
+#
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ ["Joe hit a ball, the hit ball flew far after it was hit.", "hit"],
+ [ "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", "the"],
+];
+
+sub most_frequent_word
+{
+ my ($p, $w) = @_;
+
+ $p =~ s/[^a-z\s]//gi; # keep letters only
+ my @w = split(/\s/, $p);
+ my %stat;
+ for (@w) {
+ ++$stat{$_} unless $_ eq $w;
+ }
+
+ return (sort {$stat{$b} <=> $stat{$a}} keys %stat)[0];
+}
+
+is(most_frequent_word($cases->[0]->@*), 'ball', 'Example 1');
+is(most_frequent_word($cases->[1]->@*), 'Perl', 'Example 2');
+done_testing();
+
+exit 0;
+
+