The problem is recursive function?

Good evening! There is the challenge of finding the closest values. For example have a product with a price of 12, someone to put the same price, the system must understand that a product with such a price, there are add 0.1 to the price. Went to 12.1, but if this price is busy to add another 0.1.

In a nutshell, you need to find the nearest from is entered, a free price increments of 0.1

public function findCorrectClosestPrice($price, $products)
{
 foreach ($products as $product)
{
 if ($product->price === $price)
{
 $price = $price + 0.1;
 return $this->findCorrectClosestPrice($price, $products);
}
else
{
 return $price + 0.1;
}
}

 return null;
 }


The problem is that if you already have the price of 12, 12.1 and I say put 12, the item 2 is presented with a price of 12.1, although should be installed 12.2. The question is simple, but I really don't see any errors in your code, follow the nose, I beg you.
March 23rd 20 at 19:17
2 answers
March 23rd 20 at 19:19
Solution
By the way, it is possible and without a recursion. Only when compared to do rounding, but it may be that 12.3 != 12.3 , Oh, these floating point numbers and php...
public function findCorrectClosestPrice($price, $products){
do{
 $finded = false;
 foreach ($products as $product){ 
 if (round($product->price,1) == round($price,1)){
 $price += 0.1;
 $finded = true;
break;
}
}
}while($finded);
 return $price;
}


PS Recursion is so
public function findCorrectClosestPrice($price, $products){
 foreach ($products as $product) 
 if (round($product->price,1) == round($price,1))
 return $this->findCorrectClosestPrice($price + 0.1,$products);
 return $price;
}
Yes, it will work. But the complexity is O(n^3), as I understand it. If not more. For each element, spinning and spinning cycles throughout the array instead of having to sort it in advance and to work only with its immediate neighbors. - Jesse87 commented on March 23rd 20 at 19:22
@Jesse87, I Agree, but the question was not on optimization. - coy_Bode commented on March 23rd 20 at 19:25
March 23rd 20 at 19:21
So take an array [12, 12.1, 12] and scroll your code.
You will see that your function does not what you claim.
You do not need recursion, and sorting by price upwards and nested loop.
Once in the outer loop is repeated, inner trigger to determine where to move the current item. Re-arrange, appoint him the price of the previous + 0.1 and return to the same place of the outer loop, since the array slid back due to the permutation.
Why recursion is not suitable and your solution to me looks chaotic, I probably do not fully understand how to implement your example... - Kavon commented on March 23rd 20 at 19:24
@Kavon, your recursive function finds the first price in the array that do not match that, and happily returns the price + 0.1 without checking if there are any merchandise with this price. I would have just thrown out and started thinking again. - Jesse87 commented on March 23rd 20 at 19:27
very good answer - Reba commented on March 23rd 20 at 19:30
@Reba, thank you. Can't say the same about the wealth of your imagination, unfortunately... - Jesse87 commented on March 23rd 20 at 19:33
@Jesse87, super, thank you for the answer - Reba commented on March 23rd 20 at 19:36

Find more questions by tags PHP