aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2025-09-22 10:00:31 +0200
committerE. Choroba <choroba@matfyz.cz>2025-09-22 10:00:31 +0200
commit64af807537f00aed047947952822da41577fd2ef (patch)
tree8e1818a9c8aee6c40489d8c5ac6af69469a7b6d2
parentc4e70544812c339e0344ad3127de18a5dbf98c34 (diff)
downloadperlweeklychallenge-club-64af807537f00aed047947952822da41577fd2ef.tar.gz
perlweeklychallenge-club-64af807537f00aed047947952822da41577fd2ef.tar.bz2
perlweeklychallenge-club-64af807537f00aed047947952822da41577fd2ef.zip
Solve 340: Duplicate Removals && Ascending Numbers by E. Choroba
-rwxr-xr-xchallenge-340/e-choroba/perl/ch-1.pl20
-rwxr-xr-xchallenge-340/e-choroba/perl/ch-2.pl34
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-340/e-choroba/perl/ch-1.pl b/challenge-340/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..7704ac07aa
--- /dev/null
+++ b/challenge-340/e-choroba/perl/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub duplicate_removals($str) {
+ 1 while $str =~ s/(.)\1//g;
+ return $str
+}
+
+use Test::More tests => 5 + 2;
+
+is duplicate_removals('abbaca'), 'ca', 'Example 1';
+is duplicate_removals('azxxzy'), 'ay', 'Example 2';
+is duplicate_removals('aaaaaaaa'), "", 'Example 3';
+is duplicate_removals('aabccba'), 'a', 'Example 4';
+is duplicate_removals('abcddcba'), "", 'Example 5';
+
+is duplicate_removals('aaa'), 'a', 'Odd length';
+is duplicate_removals('aabccdeedffbgg'), "", 'Many steps';
diff --git a/challenge-340/e-choroba/perl/ch-2.pl b/challenge-340/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..8df9da1ad3
--- /dev/null
+++ b/challenge-340/e-choroba/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub ascending_numbers($str) {
+ my @numbers = $str =~ /(\d+)/ag;
+ for my $i (1 .. $#numbers) {
+ return if $numbers[$i] <= $numbers[ $i - 1 ];
+ }
+ return 1
+}
+
+use constant { true => !0, false => !1 };
+use Test2::V0;
+plan(5 + 1);
+
+is ascending_numbers('The cat has 3 kittens 7 toys 10 beds'),
+ bool(true),
+ 'Example 1';
+is ascending_numbers('Alice bought 5 apples 2 oranges 9 bananas'),
+ bool(false),
+ 'Example 2';
+is ascending_numbers('I ran 1 mile 2 days 3 weeks 4 months'),
+ bool(true),
+ 'Example 3';
+is ascending_numbers('Bob has 10 cars 10 bikes'),
+ bool(false),
+ 'Example 4';
+is ascending_numbers('Zero is 0 one is 1 two is 2'),
+ bool(true),
+ 'Example 5';
+
+is ascending_numbers('There are no numbers'), bool(true), 'No numbers';