aboutsummaryrefslogtreecommitdiff
path: root/challenge-256/dave-jacoby/perl/ch-2.pl
blob: 0ca524d0cecd9bccb3e66102129ac90531a5c9c5 (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
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ say postderef signatures state };

my @examples = (

    [ "abcd",   "1234" ],
    [ "abc",    "12345" ],
    [ "abcde",  "123" ],
);

for my $example (@examples) {
    my $output = merge_strings( $example->@* );
    my $p      = $example->[0];
    my $w      = $example->[1];

    say <<~"END";
    Input:  \$str1 = "$p", \$str2 = "$w"
    Output: "$output"
    END
}

sub merge_strings ( $str1, $str2 ) {
    my $output;
    while ( length $str1 && length $str2 ) {
        $output .= substr( $str1, 0, 1 ) .  substr( $str2, 0, 1 );
        substr( $str1, 0, 1 ) = '';
        substr( $str2, 0, 1 ) = '';
    }
    return $output . $str1 . $str2;
}