aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-06-11 12:51:45 +0100
committerGitHub <noreply@github.com>2024-06-11 12:51:45 +0100
commit48b6b99909b0f1430069a64ad378475b4d63a02a (patch)
treebe89e2492883659612af50957ed022e089b01ff7
parentd2303f5f78b3edb7f5a9935be5bde2e9c7f6fd31 (diff)
parenta972a7f4601bd5647610a89df4858840e6fd9390 (diff)
downloadperlweeklychallenge-club-48b6b99909b0f1430069a64ad378475b4d63a02a.tar.gz
perlweeklychallenge-club-48b6b99909b0f1430069a64ad378475b4d63a02a.tar.bz2
perlweeklychallenge-club-48b6b99909b0f1430069a64ad378475b4d63a02a.zip
Merge pull request #10240 from PerlBoy1967/branch-for-challenge-273
w273 - Task 1 & 2
-rwxr-xr-xchallenge-273/perlboy1967/perl/ch1.pl36
-rwxr-xr-xchallenge-273/perlboy1967/perl/ch2.pl37
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-273/perlboy1967/perl/ch1.pl b/challenge-273/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..1b63f8c4ec
--- /dev/null
+++ b/challenge-273/perlboy1967/perl/ch1.pl
@@ -0,0 +1,36 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 273
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-273
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Percentage of Character
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str and a character $char.
+
+Write a script to return the percentage, nearest whole, of given
+character in the given string.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Test2::V0;
+
+sub percentageOfString ($str,$char) {
+ my %f; $f{$_}++ for (split(//,$str));
+ int(0.5 + 100 * ($f{$char} // 0) / length($str));
+}
+
+is(percentageOfString('perl','e'),25,'Example 1');
+is(percentageOfString('java','a'),50,'Example 2');
+is(percentageOfString('python','m'),0,'Example 3');
+is(percentageOfString('ada','67'),0,'Example 4');
+
+done_testing;
diff --git a/challenge-273/perlboy1967/perl/ch2.pl b/challenge-273/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..db7ee646d1
--- /dev/null
+++ b/challenge-273/perlboy1967/perl/ch2.pl
@@ -0,0 +1,37 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 273
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-273
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: B After A
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str.
+
+Write a script to return true if there is at least one 'b', and
+no 'a' appears after the first 'b'.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+use common::sense;
+
+use Data::Printer;
+
+use Test2::V0;
+
+sub bAfterA ($str) {
+ 0 + ($str =~ m#^[^b]*b[^a]*$#);
+}
+
+is(bAfterA('aabb'),1,'Example 1 (aabb)');
+is(bAfterA('abab'),0,'Example 2 (abab)');
+is(bAfterA('aaa'),0,'Example 3 (aaa)');
+is(bAfterA('bbb'),1,'Example 3 (bbb)');
+
+done_testing;