diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2024-08-12 22:35:44 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2024-08-12 22:35:44 +0000 |
| commit | 73d9ef832cd8bdfb74df03d00272d486e94728bf (patch) | |
| tree | 5f72a94da72558a6a2bc1b1d8f730c0a8f4a6034 | |
| parent | 6bc37eb88f4850e1f6fc02a038163fb382f296dc (diff) | |
| download | perlweeklychallenge-club-73d9ef832cd8bdfb74df03d00272d486e94728bf.tar.gz perlweeklychallenge-club-73d9ef832cd8bdfb74df03d00272d486e94728bf.tar.bz2 perlweeklychallenge-club-73d9ef832cd8bdfb74df03d00272d486e94728bf.zip | |
w282 - Task 1 & 2
| -rwxr-xr-x | challenge-282/perlboy1967/perl/ch1.pl | 43 | ||||
| -rwxr-xr-x | challenge-282/perlboy1967/perl/ch2.pl | 39 |
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; |
