All pastes #2072889 Raw Edit

Stuff

public php v1 · immutable
#2072889 ·published 2011-05-31 18:13 UTC
rendered paste body
<?php  2   3 /**  4  * Facebook Engineering Puzzle #1  5  * Hoppity Hippity!  6  * https://www.facebook.com/careers/puzzles.php?puzzle_id=7   7  * 10  * 11  */ 12  13 if (empty($argv[1])) { 14     exit("\n\tUsage: `php {$argv[0]} [input filename]`\n\n"); 15 } 16  17 $filename = $argv[1]; 18  19 if (!file_exists($filename)){ 20     exit("File does not exist: {$filename}\n"); 21 } 22  23 if (!is_readable($filename)) { 24     exit("File is not readable.  Check permissions.\n"); 25 } 26  27 $fh = fopen($filename, "rb") 28     or exit("Failed to open file: {$filename}\n"); 29  30 $input_line = trim(fgets($fh)); 31  32 fclose($fh); 33  34 $iterations = intval($input_line); 35  36 if (!is_int($iterations)) { 37     exit("File ({$filename}) content must be an integer.\n"); 38 } 39  40 for ($i = 1; $i <= $iterations; $i++) { 41     if ($output = hoppity($i)) { 42  43         echo $output, "\n"; 44  45     } 46 } 47  48 function hoppity($n) { 49  50     if ($n % 15 === 0) { 51  52         return 'Hop'; 53  54     } elseif ($n % 3 === 0) { 55  56         return 'Hoppity'; 57  58     } elseif ($n % 5 === 0) { 59  60         return 'Hophop'; 61  62     } 63 } 64