aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-11 12:53:42 +0100
committerGitHub <noreply@github.com>2024-06-11 12:53:42 +0100
commit4aec4c9ba5afcfdd64e089272280667dfa356caa (patch)
tree28d19bace7c1f8bd4625d122d79d21e1d5508005
parent48b6b99909b0f1430069a64ad378475b4d63a02a (diff)
parent15b218f8d0a57e1b1b1f97c2c804ce8f4fdce5a4 (diff)
downloadperlweeklychallenge-club-4aec4c9ba5afcfdd64e089272280667dfa356caa.tar.gz
perlweeklychallenge-club-4aec4c9ba5afcfdd64e089272280667dfa356caa.tar.bz2
perlweeklychallenge-club-4aec4c9ba5afcfdd64e089272280667dfa356caa.zip
Merge pull request #10241 from choroba/ech273
Solve 273: Percentage of Character & B After A by E. Choroba
-rwxr-xr-xchallenge-273/e-choroba/perl/ch-1.pl20
-rwxr-xr-xchallenge-273/e-choroba/perl/ch-2.pl22
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-273/e-choroba/perl/ch-1.pl b/challenge-273/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..7346da2e0f
--- /dev/null
+++ b/challenge-273/e-choroba/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use POSIX qw{ round };
+
+sub percentage_of_character($str, $char) {
+ my $count =()= $str =~ /\Q$char/g;
+ return round(100 * $count / length $str)
+}
+
+use Test::More tests => 6;
+
+is percentage_of_character('perl', 'e'), 25, 'Example 1';
+is percentage_of_character('java', 'a'), 50, 'Example 2';
+is percentage_of_character('python', 'm'), 0, 'Example 3';
+is percentage_of_character('ada', 'a'), 67, 'Example 4';
+is percentage_of_character('ballerina', 'l'), 22, 'Example 5';
+is percentage_of_character('analitik', 'k'), 13, 'Example 6';
diff --git a/challenge-273/e-choroba/perl/ch-2.pl b/challenge-273/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..3ebdf460d3
--- /dev/null
+++ b/challenge-273/e-choroba/perl/ch-2.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub b_after_a($str) {
+ $str =~ /^ # Start of the string.
+ [^b]* # Anything up to the first b.
+ b # The first b.
+ (?! # Not folllowed by...
+ .* # ... anything...
+ a # ... and a.
+ )
+ /x
+}
+
+use Test::More tests => 4;
+
+ok b_after_a('aabb'), 'Example 1';
+ok ! b_after_a('abab'), 'Example 2';
+ok ! b_after_a('aaa'), 'Example 3';
+ok b_after_a('bbb'), 'Example 4';