lundi 3 juillet 2017

What is correct way to do this (PHP finding largest possible number)

Ok, so i just sat an online test.

One of the questions was, given a number (ints in examples), create an algorithm to return the largest possible number from the input number. example FindLargestNumber(989) = 998

So I did this:

function GetLargestNumber($input)
{
  // Your code goes here
  $input = str_split($input, 1);
  rsort($input);
  $output = implode($input);
  return intval($output);
}

Now it worked, certainly for the unit tests, however, I am no mathematician and I expect there is a mathematical way to do this. What is that?

Aucun commentaire:

Enregistrer un commentaire