blob: 55ceac0064ac31150399916991082d4a8a83cd4e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#!/usr/bin/perl
# Perl Weekly Challenge - 115
# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-115/#TASK1
#
# Task 1 - String Chain
#
# Author: Niels 'PerlBoy' van Dijke
use v5.16;
use strict;
use warnings;
use Algorithm::Combinatorics qw(permutations);
use Test::More;
is(isStringChain(qw(abc dea cd )), 1);
is(isStringChain(qw(ade cbd fgh)), 0);
is(isStringChain(qw(ab bc ca bb)), 1);
is(isStringChain(qw(ab cd da bd)), 0);
is(isStringChain(qw(abba adda acca axxa)), 1);
is(isStringChain(qw(the weekly challenge)), 0);
done_testing;
sub isStringChain {
my (@l) = @_;
# We only need the first and last char
map { s/^(.).*?(.)$/$1$2/ } @l;
# Build regexp
my $re = qr '^(.)'.join('',map {"(.)\\$_"} (2 .. scalar @l)).'\\1$';
my $iter = permutations(\@l);
while (my $ar = $iter->next()) {
if (join('',@$ar) =~ m#$re#) {
return 1;
}
}
return 0;
}
|