Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active October 22, 2020 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/985c8c31f41697bfb5580b75a41ccf11 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/985c8c31f41697bfb5580b75a41ccf11 to your computer and use it in GitHub Desktop.
Just PHP FUN 135.
<?php
# https://www.codewars.com/kata/5842df8ccbd22792a4000245 Write Number in Expanded Form.
function expanded_form(int $n): string {
$ans = []; $power = 0;
while($n > 10){
$x = $n % 10;
array_unshift($ans,[$x,$power]);
$power += 1;
$n = intval(substr(sprintf('%d',$n),0,-1));
}
array_unshift($ans, [$n,$power]);
$ans = array_filter($ans,fn($x)=>$x[0] == 0 ? false : true);
return imploder($ans);
}
function imploder($arr){
$answer = "";
foreach($arr as $pair) $answer .= " + ".strval($pair[0]) . str_repeat("0",$pair[1]);
return trim($answer, " +");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment