0

I have tried to solve this problem: you are given two integers(A and B). You need to make an output from A to B(inclusively) in ascending order if A < B and in descending order if B < A. The problem is that it has to be done using a recursive function. I have come up with the following:

function showAB($a, $b) {
    if ($a > $b) {
        if ($a == $b) {
            return $a;
        } else {
            return $a . "<br>" . showAB($a - 1, $b);
        }
    } 
    else {
        if ($a == $b) {
            return $a;
        } else {
            return $a . "<br>" . showAB($a + 1, $b);
        }
    }
}

However, I was told that the solution to this problem can be done in 2 lines of code or less, so my question is about optimizing the function I already have.

4
  • "optimizing" isn't allways squeesing everything in 2 lines... There is code that can never be reached though (line 4) Commented Feb 26, 2019 at 23:29
  • beware of the word "optimizing". When looking at your code now, it's readable and easy to follow logically. An "optimized" solution with fewer lines of code may be a single function that has a single line of code: return ($a > $b) ? $a."<br>".showAB($a-1, $b) : ($a == $b) ? $a : $a."<br>".showAB($a+1, $b); I haven't tested this, but this should execute the same as the code above and is only a single line! But it's nowhere near as readable as what you have... Commented Feb 26, 2019 at 23:29
  • 3
    As @WOUNDEDStevenJones says, optimising often makes code a lot less readable. Here's a pretty simple version though: return ($a != $b) ? $a . '<br>' . showAB($a + ($a < $b ? 1 : -1), $b) : $a . '<br>'; 3v4l.org/bba8k Commented Feb 26, 2019 at 23:31
  • He has logic in his function what will never run like within his first if statement, $a will never == $b because if $a > $b they are obviously not equal to each other Commented Feb 26, 2019 at 23:40

2 Answers 2

1

You can still optimise without making it look ugly, especially removing pointless logic. If you want a 1 liner then refer to the comments otherwise you can remove your else statements and it's still easy to read:

function showAB($a, $b) {
    if ($a < $b) {
        return $a . "<br>" . showAB($a + 1, $b);
    }
    if ($a > $b) {
      return $a . "<br>" . showAB($a - 1, $b);
    } 
    return $a;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Golf? You could use the ternary operator ?..: and the 'spaceship' operator <=> giving -1/0/1 for less than/equal/greater than

function showAB(int $a, int $b) : string
{
  return $a . '<br>' . PHP_EOL . ($a === $b ? '' : showAB($a - ($a<=>$b), $b));
}

1 Comment

I know what PHP_EOL does, but I would like to know why you decided to use it and is it necessary or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.