如何根据计数动态合并两组数据/数组

时间:2022-09-03 15:57:28

So, I have this requirement that I have two sets of data (stored in arrays, but can be anything). What I want to do is that I want to add these two sets together so that the final result count is 10.

所以,我有这个要求,我有两组数据(存储在数组中,但可以是任何东西)。我想要做的是我想将这两个集合添加到一起,以便最终结果计数为10。

The scenarios are:

场景是:

  1. Both sets can initially have more than 5 (or 10 for that matter). In which case it is easy - I just take 5 from each set and add them together and display

    两组最初可以有5个以上(或者10个)。在这种情况下它很容易 - 我只需从每组中取5并将它们一起添加并显示

  2. Either set could be less than 5. In which case, I should take whatever is available in that set. In the other set, I should take how-much-ever is required to make total 10, If the other set's count is low such that taking it does not make total to 10, then I should take it all and display whatever I got.

    任何一个都可以小于5.在这种情况下,我应该采取该集合中可用的任何内容。在另一组中,我需要花费多少时间才能使总数达到10,如果另一组的数量很少,那么它的总数不会达到10,那么我应该把它全部拿出去展示我得到的东西。

Based on this requirement, I am trying to write a logic which will give me the count required from each set, however the if-else-if-else is getting too complicated that I think I might be doing it incorrectly. Can anyone help me with creating a simpler logic to do what I need?

基于这个要求,我试图编写一个逻辑,它将给我每个集合所需的计数,但if-else-if-else变得太复杂,我认为我可能做错了。任何人都可以帮助我创建一个更简单的逻辑来做我需要的吗?

My current (incomplete and convoluted) logic is:

我目前的(不完整和错综复杂的)逻辑是:

if($set1Count >= 5)
{
    $requiredSet1Count = 5;
    if($set2Count >= 5)
    {
        $requiredSet2Count = 5;
    }
    else
    {
        $requiredSet2Count = $set2Count;
        if($requiredSet1Count > = (10 - $set2Count))
        {
            $requiredSet1Count = (10 - $set2Count);
        }
        else
        {
            $requiredSet1Count = $set1Count;
        }
    }
}
else
{
    .....// I gave up by the time I reached here....
}

In above code $set1Count and $set2Count are the actual result counts in the two sets/arrays. The $requiredSet1Countand $requiredSet2Count are the dynamic counts I need which will tell me how many elements to extract from each set.

在上面的代码中,$ set1Count和$ set2Count是两个集/数组中的实际结果计数。 $ requiredSet1Count和$ requiredSet2Count是我需要的动态计数,它将告诉我从每个集合中提取多少元素。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

1 个解决方案

#1


I don't know a variant without ifs. Lets try to use one for every situation

我不知道没有ifs的变种。让我们尝试在每种情况下使用一个

function requiredSet($set1count, $set2count) {
// Both arrays together contain less than 10 items
    if ($set1count + $set2count <= 10) { 
        $requiredSet1Count  = $set1count;  $requiredSet2Count = $set2count;
    }
// 1st less than 5 elements
    elseif ($set1count < 5) {
        $requiredSet1Count  = $set1count;
        $requiredSet2Count  = $set2count + $set1count > 10 ? 10 - $set1count : $set2count;
    }
// 2nd - less than 5 elements
    elseif ($set2count < 5) {
        $requiredSet2Count  = $set2count;
        $requiredSet1Count  = $set1count + $set2count > 10 ? 10 - $set2count : $set1count;
    }
// Just take 5 elements in each
    else $requiredSet1Count = $requiredSet2Count = 5;

    return array($requiredSet1Count, $requiredSet2Count);
}

echo '<pre>';
var_export(requiredSet(1,2)); echo '<br>';
var_export(requiredSet(2,5)); echo '<br>';
var_export(requiredSet(2,7)); echo '<br>';
var_export(requiredSet(2,13)); echo '<br>';
var_export(requiredSet(13,11)); echo '<br>';

result

array ( 0 => 1, 1 => 2)
array ( 0 => 2, 1 => 5)
array ( 0 => 2, 1 => 7)
array ( 0 => 2, 1 => 8)
array ( 0 => 5, 1 => 5)

#1


I don't know a variant without ifs. Lets try to use one for every situation

我不知道没有ifs的变种。让我们尝试在每种情况下使用一个

function requiredSet($set1count, $set2count) {
// Both arrays together contain less than 10 items
    if ($set1count + $set2count <= 10) { 
        $requiredSet1Count  = $set1count;  $requiredSet2Count = $set2count;
    }
// 1st less than 5 elements
    elseif ($set1count < 5) {
        $requiredSet1Count  = $set1count;
        $requiredSet2Count  = $set2count + $set1count > 10 ? 10 - $set1count : $set2count;
    }
// 2nd - less than 5 elements
    elseif ($set2count < 5) {
        $requiredSet2Count  = $set2count;
        $requiredSet1Count  = $set1count + $set2count > 10 ? 10 - $set2count : $set1count;
    }
// Just take 5 elements in each
    else $requiredSet1Count = $requiredSet2Count = 5;

    return array($requiredSet1Count, $requiredSet2Count);
}

echo '<pre>';
var_export(requiredSet(1,2)); echo '<br>';
var_export(requiredSet(2,5)); echo '<br>';
var_export(requiredSet(2,7)); echo '<br>';
var_export(requiredSet(2,13)); echo '<br>';
var_export(requiredSet(13,11)); echo '<br>';

result

array ( 0 => 1, 1 => 2)
array ( 0 => 2, 1 => 5)
array ( 0 => 2, 1 => 7)
array ( 0 => 2, 1 => 8)
array ( 0 => 5, 1 => 5)