Created
July 15, 2015 12:02
-
-
Save JFFail/942c9137198aa79e8480 to your computer and use it in GitHub Desktop.
Solution to Reddit Daily Programmer #223 - Garland
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use warnings; | |
use strict; | |
sub garland { | |
use integer; | |
my $word = shift; | |
print "$word - "; | |
my @word_array = split("", $word); | |
my $length = @word_array; | |
my $start = 2; | |
while($start < $length) { | |
my $match = 1; | |
for(my $i = 0; $i < ($length - $start); $i++) | |
{ | |
my $value = $start + $i; | |
if($word_array[$i] ne $word_array[$start + $i]) { | |
$match = 0; | |
last; | |
} | |
} | |
if($match) { | |
my $final = $length - $start; | |
print "$final\n"; | |
last; | |
} | |
if($start + 1 == $length) | |
{ | |
print "No match\n"; | |
} | |
$start++; | |
} | |
} | |
garland("programmer"); | |
garland("onion"); | |
garland("alfalfa"); | |
garland("ceramic"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment