diff options
| author | E. Choroba <choroba@matfyz.cz> | 2023-05-01 23:31:39 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2023-05-01 23:32:28 +0200 |
| commit | 6fe30982ea29f09a1d01c69d5c4fa791b53f96cd (patch) | |
| tree | 6db40a362a6fd05fe9a9eb15b6509bbf44589b86 | |
| parent | 6cc2e38f43011f65d7deaf1e03cf55e4306a53e5 (diff) | |
| download | perlweeklychallenge-club-6fe30982ea29f09a1d01c69d5c4fa791b53f96cd.tar.gz perlweeklychallenge-club-6fe30982ea29f09a1d01c69d5c4fa791b53f96cd.tar.bz2 perlweeklychallenge-club-6fe30982ea29f09a1d01c69d5c4fa791b53f96cd.zip | |
Add solutions to 215: Odd one Out & Number Placement by E. Choroba
| -rwxr-xr-x | challenge-215/e-choroba/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-215/e-choroba/perl/ch-2.pl | 16 |
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-215/e-choroba/perl/ch-1.pl b/challenge-215/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..6aaa049639 --- /dev/null +++ b/challenge-215/e-choroba/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub odd_one_out(@words) { + scalar grep { + my $word = $_; + grep substr($word, $_ - 2, 1) gt substr($word, $_ - 1, 1), + 2 .. length $word + } @words +} + +use Test::More tests => 3; + +is odd_one_out('abc', 'xyz', 'tsu'), 1, 'Example 1'; +is odd_one_out('rat', 'cab', 'dad'), 3, 'Example 2'; +is odd_one_out('x', 'y', 'z'), 0, 'Example 3'; diff --git a/challenge-215/e-choroba/perl/ch-2.pl b/challenge-215/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..3d90d73307 --- /dev/null +++ b/challenge-215/e-choroba/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub number_placement($list, $count) { + (() = "@$list" =~ /0 0(?= 0)/g) >= $count ? 1 : 0 +} + +use Test::More tests => 3 + 1; +is number_placement([1, 0, 0, 0, 1], 1), 1, 'Example 1'; +is number_placement([1, 0, 0, 0, 1], 2), 0, 'Example 2'; +is number_placement([1, 0, 0, 0, 0, 0, 0, 0, 1], 3), 1, 'Example 3'; + +# Why /0(?= 0 0)/ is wrong: +is number_placement([1, 0, 0, 0, 0, 0, 0, 1], 3), 0, 'Edge case'; |
