- https://www.codewars.com/kata/5324945e2ece5e1f32000370 Sum Strings as Numbers.
Started at 22:36 at 08.10.2020 at Thursday Oct.
Finished at 23:04 at 08.10.2020 at Thursday Oct. (0hrs 28minutes)
Started at 22:36 at 08.10.2020 at Thursday Oct.
Finished at 23:04 at 08.10.2020 at Thursday Oct. (0hrs 28minutes)
<?php | |
# https://www.codewars.com/kata/5324945e2ece5e1f32000370 Sum Strings as Numbers. | |
function sum_strings($a, $b) { | |
return $a + $b; | |
} | |
# ----------------------------------------------------------------------------- | |
function sum_strings($a, $b) { | |
$max = max(strlen($a),strlen($b)); | |
/*echo "Start. $max; \n"; | |
echo "$a \n"; | |
echo "$b \n";*/ | |
$a = to_a($a,$max); | |
$b = to_a($b,$max); | |
$ans = ""; | |
$nxt = 0; | |
for($i = 0; $i < $max; $i += 1){ | |
$tmp = intval($a[$i]) + intval($b[$i]) + $nxt; | |
$nxt = $tmp >= 10 ? 1 : 0; | |
$ans = strval($tmp%10).$ans; | |
} | |
if(1 == $nxt) $ans = "1".$ans; | |
if(1 < strlen($ans)) $ans = preg_replace('/^0+/',"",$ans); | |
//echo "$ans \n"; | |
return $ans; | |
} | |
function to_a($str,$max){ | |
$padd = str_pad($str,$max,"0",STR_PAD_LEFT); | |
return array_reverse(str_split($padd)); | |
} |