I love puzzles, especially coding puzzles. Here is one that I found on Facebook’s jobs page for Hoppity Hop!. It is similar to one I had to solve during a job interview last year but it’s a little different because this one has to be run via the Command Line Interface. This is the easiest puzzle that Facebook posted, if you want to see the other puzzles, head on over to: http://www.facebook.com/careers/puzzles.php.
Here is Facebook’s description for the puzzle:
To help test whether your puzzle submissions fit the guidelines, try this simple test puzzle. Your solution must follow the guidelines like any other puzzle. Write a program that takes as input a single argument on the command line. This argument must be a file name, which contains a single positive integer. The program should read this file and obtain the integer within, and then output a sequence of strings based upon the number (details below).
Here is my solution to the puzzle:
<?php
/*
Huy Tong
Facebook Hoppity Hop! puzzle
*/
/* make sure that an argument was passed */
if (isset($argv[1])) {
/* argument 1 should be the file name */
$file = $argv[1];
/* check if that file exists */
if (file_exists($file)) {
/* get the number inside the file */
$n = (int)trim(file_get_contents($file));
/* check if the file containted the right data type */
if (is_numeric($n)) {
for ($i=1;$i<$n;$i++) {
if ((($i % 3) == 0) && (($i % 5) == 0)) {
/* the number is divisible by 3 and 5 */
print "Hop\n";
} else if (($i % 3) == 0) {
/* the number is divisible by 3 */
print "Hoppity\n";
} else if (($i % 5) == 0) {
/* the number is divisible by 5 */
print "Hophop\n";
} else {
/* do nothing */
}
}
} else {
print "The file must contain an integer.\n";
}
} else {
print "The file you are trying to access, ".$file.", does not exist.\n";
}
} else {
print "Please enter a file name to use in the argument.\n";
}
?>
Here is the sample data file, I named it num.txt but you can name it whatever you want because it is called via the command line.
15
hello!! is it anyone? I need your help.. can you write this puzzle on assembly? tasm? please answer.. :))
July 23, 2009