aboutsummaryrefslogtreecommitdiff
path: root/challenge-020
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-16 22:44:14 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-03-23 18:03:42 +0100
commit312cd5841215a5f45a07c88bffeabaa0e166d734 (patch)
tree112f43ddb83f28cff33c37dfba3b09ff854d42b5 /challenge-020
parent95677ee25bcfc34fbfea5d5dc570e06f3872788c (diff)
downloadperlweeklychallenge-club-312cd5841215a5f45a07c88bffeabaa0e166d734.tar.gz
perlweeklychallenge-club-312cd5841215a5f45a07c88bffeabaa0e166d734.tar.bz2
perlweeklychallenge-club-312cd5841215a5f45a07c88bffeabaa0e166d734.zip
Challenge 020 task 1
Diffstat (limited to 'challenge-020')
-rwxr-xr-xchallenge-020/jo-37/perl/ch-1.pl59
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-020/jo-37/perl/ch-1.pl b/challenge-020/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..9b0ddcb19a
--- /dev/null
+++ b/challenge-020/jo-37/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STR
+ String to be split into groups of repeated characters
+
+EOS
+
+
+### Input and Output
+
+say for @{split_string(shift)};
+
+
+### Implementation
+
+sub split_string {
+ # Capture each repeated single character. There are two capture
+ # groups in this regex: one captures the character and the other the
+ # repeated character. Using embedded code to collect the repeated
+ # chars.
+ my @split;
+ shift =~ /(((.)\g{-1}*)(?{push @split, $^N}))*/;
+ \@split;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is split_string('ABBCDEEF'),
+ [qw(A BB C D EE F)], 'example';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}