Php 在数组中搜索最接近0的负值和正值
我需要找到数组中最接近零的负值和正值Php 在数组中搜索最接近0的负值和正值,php,arrays,Php,Arrays,我需要找到数组中最接近零的负值和正值 如果数组为空我们返回0 例如,如果数组有-7,7我们返回7 这是正确的方法吗 $ts = [1.7, 7, -10, 13, 8.4, -7.2, -12, -3.7, 3.5, -9.6, 6.5, -1.7, -6.2, 7]; function closestToZero (array $ts) { if(empty($ts)){ return 0; } $negativeArr = []; $
- 如果数组
我们为空
返回0
- 例如,如果数组有
我们-7,7
返回7
$ts = [1.7, 7, -10, 13, 8.4, -7.2, -12, -3.7, 3.5, -9.6, 6.5, -1.7, -6.2, 7];
function closestToZero (array $ts)
{
if(empty($ts)){
return 0;
}
$negativeArr = [];
$postiveValue = [];
foreach ($ts as $number) {
if ($number < 0) {
$negativeArr[] = $number;
}elseif ($number > 0 ) {
$postiveValue[] = $number;
}
}
if(!empty($negativeArr)){
$minnegative = max($negativeArr);
}
if (!empty($postiveValue)) {
$minPositive = min($postiveValue);
}
if ((abs($minnegative) - $minPositive) == 0) {
return $minPositive;
}else{
return $minnegative.' '.$minPositive;
}
}
您可以用更少的代码完成此操作:
<?php
function getClosest(array $x)
{
// put them in order first
sort($x);
$results = [];
foreach($x as $y) {
if ($y < 0) {
$results['-'] = $y; // next negative is closer to 0
} else {
$results['+'] = $y; // first positive is closest to 0
return $results;
}
}
return count($results) > 0 ? $results : 0;
}
$x = [1.7, 7, -10, 13, 8.4, -7.2, -12, -3.7, 3.5, -9.6, 6.5, -1.7, -6.2, 7];
$y = getClosest($x);
var_dump($y);
花了我一段时间,但我解决了它。
此函数不仅查找最接近0的正数,而且在计算中还取任何其他点
def closest_positive_to_zero(list_of_temperatures, needed_point):
print(f"got: {list_of_temperatures}") #[7, -10, 4, -7.2, 1.7, -1.7, -6.2, 1.7, 1.8, 1.7, 10]
sorted_list = sorted(set(list_of_temperatures), key=lambda temperature: abs(temperature-needed_point)) # used a set to get rid of dublicates, sorted with lambda closest to 0
print(f"sorted: {sorted_list}") #[1.7, -1.7, 1.8, 4, -6.2, 7, -7.2, 10, -10]
return sorted_list[1] if sorted_list[0] < sorted_list[1] and abs(sorted_list[0]) == abs(sorted_list[1]) else sorted_list[0] # if there are two first same closest numbers (+1.7 and -1.7) take the positive one
list_of_temperatures = [7, -10, 4, -7.2, 1.7, -1.7, -6.2, 1.7, 1.8, 1.7, 10]
the_closest = closest_positive_to_zero(list_of_temperatures, 0)
print(f"{the_closest}")
def最接近零度(列出所需的温度点):
print(f“got:{list_of_temperatures}”)#[7,-10,4,-7.2,1.7,-1.7,-6.2,1.7,1.8,1.7,10]
已排序的列表=已排序的(设置(列表中的温度),键=lambda温度:abs(需要的温度点))\
打印(f“排序:{sorted_list}”)#[1.7,-1.7,1.8,4,-6.2,7,-7.2,10,-10]
如果排序后的列表[0]<排序后的列表[1]和abs(排序后的列表[0])==abs(排序后的列表[1]),则返回排序后的列表[1],否则排序后的列表[0];如果前面有两个相同的最近数(+1.7和-1.7),则取正数
温度列表=[7,-10,4,-7.2,1.7,-1.7,-6.2,1.7,1.8,1.7,10]
最接近=最接近零的正温度(列出温度,0)
打印(f“{最近的}”)
我想您无需询问可能的副本。如果它起作用,那么它是一种正确的方法。如果你想优化它,那是另一个问题。可能更适合排序在哪里?也许通过分类它会起作用。尝试使用$array=[-5,2.1,1.9,-4,3,7]代码>并将给出错误的[positive]=>2.1
。它像一个charrm一样工作,但它不能完全解决问题,因为我们希望数字是这样的重新计算值:echo“Result is”。最接近的(x元);;此外,它还应该在开始时处理空数组的情况。关于空数组,它将跳过循环,并在结尾返回0,在开始时添加额外的ifs是一种微观优化。至于返回的内容,是否应该比较负数组和正数组,而只比较最接近的返回值?如果(如示例)它们除了极性之外都是相同的呢?应该返回哪个号码?
array(2) { ["-"]=> float(-1.7) ["+"]=> float(1.7) }
<?php
$array = [-5, 2, -4, 3, 7];
$positive = array_filter($array, function ($v) {
return $v > 0;
});
$negative = array_filter($array, function ($v) {
return $v < 0;
});
print_r(['positive' => array_values($positive)[0], 'negative' => end($negative)]);
def closest_positive_to_zero(list_of_temperatures, needed_point):
print(f"got: {list_of_temperatures}") #[7, -10, 4, -7.2, 1.7, -1.7, -6.2, 1.7, 1.8, 1.7, 10]
sorted_list = sorted(set(list_of_temperatures), key=lambda temperature: abs(temperature-needed_point)) # used a set to get rid of dublicates, sorted with lambda closest to 0
print(f"sorted: {sorted_list}") #[1.7, -1.7, 1.8, 4, -6.2, 7, -7.2, 10, -10]
return sorted_list[1] if sorted_list[0] < sorted_list[1] and abs(sorted_list[0]) == abs(sorted_list[1]) else sorted_list[0] # if there are two first same closest numbers (+1.7 and -1.7) take the positive one
list_of_temperatures = [7, -10, 4, -7.2, 1.7, -1.7, -6.2, 1.7, 1.8, 1.7, 10]
the_closest = closest_positive_to_zero(list_of_temperatures, 0)
print(f"{the_closest}")