aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-282/perlboy1967/perl/ch1.pl43
-rwxr-xr-xchallenge-282/perlboy1967/perl/ch2.pl39
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-282/perlboy1967/perl/ch1.pl b/challenge-282/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..94781fb362
--- /dev/null
+++ b/challenge-282/perlboy1967/perl/ch1.pl
@@ -0,0 +1,43 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 282
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-282
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Good Integer
+Submitted by: Mohammad Sajid Anwar
+
+You are given a positive integer, $int, having 3 or more digits.
+
+Write a script to return the Good Integer in the given integer or -1 if none found.
+
+|| A good integer is exactly three consecutive matching digits.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);
+
+# Task 1
+sub goodInteger ($int) {
+ while ($int =~ /(.?)(.)(\2\2)(.?)/g) {
+ return int($2.$3) if ($1 ne $2 && $2 ne $4);
+ pos($int) = length($`) + 3;
+ }
+ return -1;
+}
+
+is(goodInteger(12344456),444,'Example 1');
+is(goodInteger(1233334),-1,'Example 2');
+is(goodInteger(10020003),0,'Example 3');
+is(goodInteger(10023000),0,'Own test 1');
+is(goodInteger(22220111),111,'Own test 2');
+
+done_testing;
diff --git a/challenge-282/perlboy1967/perl/ch2.pl b/challenge-282/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..9ebfe11409
--- /dev/null
+++ b/challenge-282/perlboy1967/perl/ch2.pl
@@ -0,0 +1,39 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 282
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-282
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Changing Keys
+Submitted by: Mohammad Sajid Anwar
+
+You are given an alphabetic string, $str, as typed by user.
+
+Write a script to find the number of times user had to change the key to type
+the given string. Changing key is defined as using a key different from the last
+used key. The shift and caps lock keys won’t be counted.
+
+=cut
+
+use v5.32;
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use common::sense;
+
+use Test2::V0 qw(-no_srand);
+
+sub changingKeys ($str) {
+ $str =~ s#(?i)(.)\1#fc($1)#eg;
+ $str =~ s#(.)\1+#$1#g;
+ length($str) - 1;
+}
+
+is(changingKeys('pPeERrLl'),3,'Example 1');
+is(changingKeys('rRr'),0,'Example 2');
+is(changingKeys('GoO'),1,'Example 3');
+is(changingKeys('Own__test'),7,'Own test');
+
+done_testing;