5 Dec 2010

Facebook Puzzle - Perl - hoppity

Facebook has added Puzzles section under its careers section. You can follow this procedure to get your test puzzle submission done.

1. Save this script in a file hoppity.


#!/usr/bin/perl

use strict;
use warnings;

my $fh;

my $path = $ARGV[0];

die("Missing Filename. Run $0 \n") if(! $path);
die("File not found ") if(! -f $path);

open($fh,"<$path") || die("Error reading file");
my $number = <$fh>;

chomp($number);
$number = trim($number);

die("Invalid input $number") if($number !~ /\d+/);

$\ = "\n";
for(my $i=1;$i<=$number;$i++) {
if ($i % 3 == 0 && $i % 5 == 0) {
print "Hop";
} elsif ($i % 3 == 0) {
print "Hoppity";
} elsif ($i % 5 == 0) {
print "Hophop";
}
}

sub trim {
my ( $str) = @_;
$str =~ s/^\s*//g;
$str =~ s/\s*$//g;
return $str;
}


2. Send this file to 1051962371@fb.com with the file attached and subject as hoppity

3. After four hours facebook puzzle bot will reply you with your submission results

No comments:

Post a Comment