Skip to content

Commit fa79198

Browse files
committed
add perl solution for wk-081 ch-2
1 parent 1f3aee3 commit fa79198

File tree

1 file changed

+59
-0
lines changed
  • challenge-082/alexander-pankoff/perl

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env perl
2+
use v5.20;
3+
use utf8;
4+
use strict;
5+
use warnings;
6+
use autodie;
7+
use feature qw(say signatures);
8+
no warnings 'experimental::signatures';
9+
10+
use Pod::Usage;
11+
12+
use List::Util qw(min all any);
13+
use Scalar::Util qw(looks_like_number);
14+
15+
=pod
16+
17+
=head1 SYNOPSIS
18+
19+
Given three strings <A>, <B> and <C> this script will return whether <C> can be
20+
created by interleaving <A> and <B>
21+
22+
=head1 USAGE
23+
24+
ch-2.pl <A> <B> <C>
25+
26+
=cut
27+
28+
pod2usage(
29+
-message => "$0: Need exactly three arguments",
30+
-exitval => 1,
31+
-verbose => 99,
32+
-sections => "USAGE|SYNOPSIS",
33+
) if @ARGV != 3;
34+
35+
my ( $A, $B, $C ) = @ARGV;
36+
say is_creatable_by_interleaving( $C, $A, $B );
37+
38+
sub is_creatable_by_interleaving ( $target, $a, $b ) {
39+
return 0 if length($target) != length($a) + length($b);
40+
return 1 if !length($target);
41+
42+
my $head = substr( $target, 0, 1 );
43+
my $rest = substr( $target, 1 );
44+
45+
return (
46+
starts_with( $head, $a )
47+
? is_creatable_by_interleaving( $rest, substr( $a, 1 ), $b )
48+
: 0
49+
)
50+
|| (
51+
starts_with( $head, $b )
52+
? is_creatable_by_interleaving( $rest, $a, substr( $b, 1 ) )
53+
: 0
54+
);
55+
}
56+
57+
sub starts_with ( $char, $str ) {
58+
return $str =~ m/^$char/;
59+
}

0 commit comments

Comments
 (0)