Skip to content

Instantly share code, notes, and snippets.

@zeroem
Created September 11, 2012 07:32
Show Gist options
  • Save zeroem/3696687 to your computer and use it in GitHub Desktop.
Save zeroem/3696687 to your computer and use it in GitHub Desktop.
with error handling
#!/usr/bin/env php
<?php
if(!isset($argv[1])) {
echo 'Usage: ' . $argv[0] . ' <dddd>';
} else if(4 !== strlen($argv[1])) {
trigger_error('Input must be only 4 digits');
} else if(false !== strpos($argv[1],'4')) {
trigger_error('No body likes 4s anyway');
} else {
$map = array(
'0'=>1,
'1'=>0,
'2'=>0,
'3'=>0,
// 4 is not accounted for
'5'=>0,
'6'=>1,
'7'=>0,
'8'=>2,
'9'=>1
);
$result = 0;
foreach(str_split($argv[1],1) as $digit) {
if(array_key_exists($digit, $map)) {
$result += $map[$digit];
}
}
echo $result . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment