diff options
| -rwxr-xr-x | challenge-184/e-choroba/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-184/e-choroba/perl/ch-2.pl | 27 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-184/e-choroba/perl/ch-1.pl b/challenge-184/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..00e84dd0bc --- /dev/null +++ b/challenge-184/e-choroba/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub sequence_number ($list) { + die "Not enough digit combinations.\n" if @$list > 100; + my $i = 0; + return [map s/^../sprintf '%02d', $i++/re, @$list]; +} + +use Test2::V0; +plan 4; + +is sequence_number(['ab1234', 'cd5678', 'ef1342']), + ['001234', '015678', '021342'], + 'Example 1'; + +is sequence_number(['pq1122', 'rs3334']), + ['001122', '013334'], + 'Example 2'; + +my $seq1 = sequence_number(generate_ids(100)); +is $seq1->[-1], '991234', 'Last possible element'; + +my $exception = dies { sequence_number(generate_ids(101)) }; +like $exception, qr/Not enough digit combinations/, 'Too long'; + +no warnings 'experimental'; # Turned off by Test2::V0. +sub generate_ids ($size) { + my $id = 'aa'; + return [map $id++ . '1234', 1 .. $size] +} diff --git a/challenge-184/e-choroba/perl/ch-2.pl b/challenge-184/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..28948e4231 --- /dev/null +++ b/challenge-184/e-choroba/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::MoreUtils qw{ part }; + +sub split_array (@strings) { + my @results; + for my $string (@strings) { + my ($digits, $letters) = part { /[a-z]/ } split ' ', $string; + push @{ $results[0] }, $digits if $digits; + push @{ $results[1] }, $letters if $letters; + } + return \@results +} + +use Test2::V0; +plan 2; + +is split_array( 'a 1 2 b 0', '3 c 4 d'), + [[[1,2,0], [3,4]], [['a','b'], ['c','d']]], + 'Example 1'; + +is split_array('1 2', 'p q r', 's 3', '4 5 t'), + [[[1,2], [3], [4,5]], [['p','q','r'], ['s'], ['t']]], + 'Example 2'; |
