aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-340/perlboy1967/perl/ch1.pl36
-rwxr-xr-xchallenge-340/perlboy1967/perl/ch2.pl43
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-340/perlboy1967/perl/ch1.pl b/challenge-340/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..144c14472c
--- /dev/null
+++ b/challenge-340/perlboy1967/perl/ch1.pl
@@ -0,0 +1,36 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-340#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Duplicate Removals
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str, consisting of lowercase English letters.
+
+Write a script to return the final string after all duplicate removals have
+been made. Repeat duplicate removals on the given string until we no longer can.
+
+|| A duplicate removal consists of choosing two adjacent and equal letters andi
+|| removing them.
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+sub removeDuplicates ($str) {
+ 1 while ($str =~ s#(.)(\1)##);
+ $str;
+}
+
+is(removeDuplicates('abbaca'),'ca','Example 1');
+is(removeDuplicates('azxxzy'),'ay','Example 2');
+is(removeDuplicates('aaaaaaaa'),'','Example 3');
+is(removeDuplicates('aabccba'),'a','Example 4');
+is(removeDuplicates('abcddcba'),'','Example 5');
+
+done_testing;
diff --git a/challenge-340/perlboy1967/perl/ch2.pl b/challenge-340/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..77910075ab
--- /dev/null
+++ b/challenge-340/perlboy1967/perl/ch2.pl
@@ -0,0 +1,43 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-340#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Ascending Numbers
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str, is a list of tokens separated by a single space. Every token
+is either a positive number consisting of digits 0-9 with no leading zeros, or a word
+consisting of lowercase English letters.
+
+Write a script to check if all the numbers in the given string are strictly increasing from
+left to right.
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+use boolean;
+use List::MoreUtils qw(slide);
+
+sub containsAscendingNumbers ($str) {
+ #boolean ((grep { $_ >= 0 } slide { $a <=> $b } grep /\d/, split(/\D+/,$str)) == 0);
+ boolean !grep { $_ >= 0 } slide { $a <=> $b } grep /\d/, split(/\D+/,$str);
+}
+
+is(containsAscendingNumbers('The cat has 3 kittens 7 toys 10 beds'),
+ true,'Example 1');
+is(containsAscendingNumbers('Alice bought 5 apples 2 oranges 9 bananas'),
+ false,'Example 2');
+is(containsAscendingNumbers('I ran 1 mile 2 days 3 weeks 4 months'),
+ true,'Example 3');
+is(containsAscendingNumbers('Bob has 10 cars 10 bikes'),
+ false,'Example 4');
+is(containsAscendingNumbers('Zero is 0 one is 1 two is 2'),
+ true,'Example 5');
+
+done_testing;