Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created July 15, 2015 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/942c9137198aa79e8480 to your computer and use it in GitHub Desktop.
Save JFFail/942c9137198aa79e8480 to your computer and use it in GitHub Desktop.
Solution to Reddit Daily Programmer #223 - Garland
#!/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