We're a place where coders share, stay up-to-date and grow their careers.
This is the solution in PHP, but I'm not sure if it's 100% correct since there aren't enough test cases and the test cases provided above don't have the result so they're pretty much useless. anyway, here's my attempt:
function grille($message, $code) { $letters = ''; $code = array_filter(str_split(sprintf("%06d", decbin($code))), function($x) { return $x == 1; }); $code = array_keys($code); foreach(str_split($message) as $index => $letter){ if(in_array($index, $code)){ $letters .= $letter; } } return $letters; }
This is the solution in PHP, but I'm not sure if it's 100% correct since there aren't enough test cases and the test cases provided above don't have the result so they're pretty much useless.
anyway, here's my attempt: