Skip to content

Commit d5672fa

Browse files
authored
Merge pull request #13236 from mattmartini/branch-for-challenge-352
Solutions for challenge #352
2 parents 105b0f6 + e009881 commit d5672fa

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env perl
2+
3+
# You are given an array of strings.
4+
5+
# Write a script to return all strings that are a substring of another
6+
# word in the given array in the order they occur.
7+
8+
use 5.018;
9+
use strict;
10+
use warnings;
11+
use Test2::V0;
12+
13+
plan tests => 5;
14+
15+
sub match_string {
16+
my (@words) = @_;
17+
diag 'Input: @words = (' . join( ', ', @words ) . ")\n";
18+
19+
my @results;
20+
my %dups;
21+
22+
MATCH:
23+
foreach ( my $i = 0; $i <= $#words; $i++ ) {
24+
next MATCH if (++$dups{$words[$i]} > 1);
25+
COMPARE:
26+
foreach ( my $j = 0; $j <= $#words; $j++ ) {
27+
next COMPARE if ( $i == $j );
28+
my $indx = index( $words[$j], $words[$i] );
29+
if ( $indx != -1 ) {
30+
push @results, $words[$i];
31+
next MATCH;
32+
}
33+
}
34+
}
35+
36+
diag 'Output: @results = (' . join( ', ', @results ) . ")\n";
37+
return \@results;
38+
}
39+
40+
is(
41+
match_string(
42+
( "cat", "cats", "dog", "dogcat", "dogcat", "rat", "ratcatdogcat" )
43+
),
44+
[ "cat", "dog", "dogcat", "rat" ],
45+
'example 1'
46+
);
47+
is(
48+
match_string(
49+
( "hello", "hell", "world", "wor", "ellow", "elloworld" )
50+
),
51+
[ "hell", "world", "wor", "ellow" ],
52+
'example 2'
53+
);
54+
is(
55+
match_string( ( "a", "aa", "aaa", "aaaa" ) ),
56+
[ "a", "aa", "aaa" ],
57+
'example 3'
58+
);
59+
is(
60+
match_string(
61+
( "flower", "flow", "flight", "fl", "fli", "ig", "ght" )
62+
),
63+
[ "flow", "fl", "fli", "ig", "ght" ],
64+
'example 4'
65+
);
66+
is(
67+
match_string(
68+
( "car", "carpet", "carpenter", "pet", "enter", "pen", "pent" )
69+
),
70+
[ "car", "pet", "enter", "pen", "pent" ],
71+
'example 5'
72+
);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env perl
2+
3+
# You are given an array, @nums, where each element is either 0 or 1.
4+
5+
# Define xi as the number formed by taking the first i+1 bits of @nums
6+
# (from $nums[0] to $nums[i]) and interpreting them as a binary number,
7+
# with $nums[0] being the most significant bit.
8+
# Write a script to return an array @answer where $answer[i] is true
9+
# if x<sub>i</sub> is divisible by 5, otherwise false.
10+
11+
use 5.018;
12+
use strict;
13+
use warnings;
14+
use Test2::V0;
15+
16+
plan tests => 5;
17+
18+
sub binary_prefix {
19+
my (@num) = @_;
20+
print 'Input: @num = (' . join( ', ', @num ) . ")\n";
21+
22+
my @answer = ();
23+
24+
MODULUS:
25+
for my $i ( 0 .. $#num ) {
26+
my $result = 'false';
27+
28+
my $binary = join( '', @num[ 0 .. $i ] );
29+
my $decimal = oct( '0b' . $binary );
30+
printf( "%12s: %d\n", $binary, $decimal );
31+
32+
if ( $decimal % 5 == 0 ) {
33+
$result = 'true';
34+
}
35+
push @answer, $result;
36+
}
37+
38+
print 'Output: @answer = (' . join( ', ', @answer ) . ")\n";
39+
return \@answer;
40+
}
41+
42+
is(
43+
binary_prefix( ( 0, 1, 1, 0, 0, 1, 0, 1, 1, 1 ) ),
44+
[ 'true', 'false', 'false', 'false', 'false', 'true',
45+
'true', 'false', 'false', 'false'
46+
],
47+
'example 1'
48+
);
49+
is(
50+
binary_prefix( ( 1, 0, 1, 0, 1, 0 ) ),
51+
[ 'false', 'false', 'true', 'true', 'false', 'false' ],
52+
'example 2'
53+
);
54+
is(
55+
binary_prefix( ( 0, 0, 1, 0, 1 ) ),
56+
[ 'true', 'true', 'false', 'false', 'true' ],
57+
'example 3'
58+
);
59+
is(
60+
binary_prefix( ( 1, 1, 1, 1, 1 ) ),
61+
[ 'false', 'false', 'false', 'true', 'false' ],
62+
'example 4'
63+
);
64+
is(
65+
binary_prefix( ( 1, 0, 1, 1, 0, 1, 0, 0, 1, 1 ) ),
66+
[ 'false', 'false', 'true', 'false', 'false', 'true',
67+
'true', 'true', 'false', 'false'
68+
],
69+
'example 5'
70+
);

0 commit comments

Comments
 (0)