aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-282/andrezgz/perl/ch-1.pl45
-rwxr-xr-xchallenge-282/andrezgz/perl/ch-2.pl38
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-282/andrezgz/perl/ch-1.pl b/challenge-282/andrezgz/perl/ch-1.pl
new file mode 100755
index 0000000000..426e031b30
--- /dev/null
+++ b/challenge-282/andrezgz/perl/ch-1.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-282/
+# Task #1 > Good Integer
+#
+# 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.
+
+# Example 1
+# Input: $int = 12344456
+# Output: "444"
+# Example 2
+# Input: $int = 1233334
+# Output: -1
+# Example 3
+# Input: $int = 10020003
+# Output: "000"
+
+use strict;
+use warnings;
+use feature 'say';
+
+use constant N => 3;
+
+my $str = shift;
+say good_integer($str);
+exit 0;
+
+sub good_integer {
+ my $int = shift;
+ # match a sequence of consecutive equal digits,
+ # capturing the first one, and the one after the sequence
+ while ($int =~ /(\d)\1+(\d?)/) {
+ # distance between the captured digits
+ my $d = $-[2] - $-[1];
+ # if the distance is N, return the sequence
+ return $1 x N if $d == N;
+ # otherwise, remove these digits and try again
+ $int = substr $int, $d;
+ }
+ return -1;
+}
diff --git a/challenge-282/andrezgz/perl/ch-2.pl b/challenge-282/andrezgz/perl/ch-2.pl
new file mode 100755
index 0000000000..73eb85d658
--- /dev/null
+++ b/challenge-282/andrezgz/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-283/
+# Task #2 > Changing Keys
+#
+# 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.
+#
+# Example 1
+# Input: $str = 'pPeERrLl'
+# Ouput: 3
+#
+# p -> P : 0 key change
+# P -> e : 1 key change
+# e -> E : 0 key change
+# E -> R : 1 key change
+# R -> r : 0 key change
+# r -> L : 1 key change
+# L -> l : 0 key change
+#
+# Example 2
+# Input: $str = 'rRr'
+# Ouput: 0
+#
+# Example 3
+# Input: $str = 'GoO'
+# Ouput: 1
+
+use strict;
+use warnings;
+use feature 'say';
+
+my $str = shift;
+
+my @chars = map {lc} split //, $str;
+my $count = grep { $chars[$_] ne $chars[$_ - 1] } 1 .. $#chars;
+say $count;