通过更改参数查找值范围的可能排列

时间:2018-06-07 06:43:17

标签: javascript c# algorithm if-statement permutation

指定操作的可能范围:

function Permutations(top2, top1, middle, bottom1, bottom2, val){
    if(val)
    {
        //all defined
        if(top2 && top1 && middle && bottom1 && bottom2)
        {
            if(val == middle)
            {
                Run();
            } 
            else if(val > middle && val < top1)
            {
                Run();
            }
            else if(val >= top1 && val < top2)
            {
                PickWeapon();
            }
            else if(val >= top2)
            {
                Jump();
            }
            else if(val < middle && val > bottom1)
            {
                Run();
            }
            else if(val <= bottom1 && val > bottom2)
            {
                DropWeapon()
            }
            else if(val <= bottom2)
            {
                Crawl();
            }

        }
        //top1 missing
        if(top2 && !top1 && middle && bottom1 && bottom2)
        {
            if(val == middle)
            {
                Run();
            } 
            else if(val > middle && val < top2) //!!! new range
            {
                Run();
            }
            else if(val >= middle && val < top2) //!!! new range
            {
                Run();
            }
            else if(val >= top2)
            {
                Jump();
            }
            else if(val < middle && val > bottom1)
            {
                Walk();
            }
            else if(val <= bottom1 && val > bottom2)
            {
                DropWeapon()
            }
            else if(val <= bottom2)
            {
                Crawl();
            }
        }
        //top1 and bottom1 missing
        if(top2 && !top1 && middle && !bottom1 && bottom2)
        {
            if(val == middle)
            {
                Run();
            } 
            else if(val > middle && val < top2) //!!! new range
            {
                Run();
            }
            else if(val >= middle && val < top2)  //!!! new range
            {
                Run();
            }
            else if(val >= top2)
            {
                Jump();
            }
            else if(val < middle && val > bottom2)  //!!! new range
            {
                Run();
            }
            else if(val <= middle && val > bottom2)  //!!! new range
            {
                Run();
            }
            else if(val <= bottom2)
            {
                Crawl();
            }
        }
        if(!top2 && top1 && middle && bottom1 && !bottom2)
        if(top2 && top1 && !middle && bottom1 && bottom2)
        if(top2 && top1 && !middle && bottom1 && !bottom2)
        ...
    }
}

问题描述:

我需要找到所有可能的排列,如果以下任何一个参数: top2,top1,middle,bottom1,bottom2 变为空 - 那么范围和行动正在改变,即

<BypassIf Property="VersionNT" Compare="ValueGreaterThanOrEqualTo" Value="6.3"/>

是否有可能为这个问题编写一个算法,所以我可以避免写if if语句?有什么建议??

由于

1 个答案:

答案 0 :(得分:0)

只需查看&#34; top1和bottom1缺少的那些部分&#34;:

if(val == middle)
{
    Run();
} 
...
else if(val > middle && val < top2) //!!! new range
{
    Run();
}
else if(val >= middle && val < top2)  //!!! new range
{
    Run();
}
...
else if(val < middle && val > bottom2)  //!!! new range
{
    Run();
}
else if(val <= middle && val > bottom2)  //!!! new range
{
    Run();
}

什么都不困扰你?

我的意思是,如果val >= middle && val < top2然后val已经== middle,那么您将永远不会达到这一点。
同样适用于val <= middle && val > bottom2

因此,您也可以写下:

if(val == middle ||                 // either val is equal to middle
   (val > middle && val < top2) ||  // or it is more than middle and less than top2
   (val < middle && val > bottom2)) // or it is less than middle and more than bottom2
{
    Run();
}

无论如何,我似乎很清楚你指定的行为是这样的:

-----bottom2----bottom1----middle ---- top1 ---- top2 ------
|       |          |                    |          |       |
| Crawl |DropWeapon|         Run        |PickWeapon| Jump  |
|_______|__________|____________________|__________|_______|

if(val <= bottom2)
    Crawl();
else if(val <= bottom1)
    DropWeapon();
else if(val < top1)
    Run();
else if(val < top2)
    PickWeapon();
else
    Jump();

如果我理解得很好,那么top1失踪,就会给出:

-----bottom2----bottom1----middle ---- top2 -----
|       |          |                    |       |
| Crawl |DropWeapon|         Run        | Jump  |
|_______|__________|____________________|_______|

if(val <= bottom2)
    Crawl();
else if(val <= bottom1)
    DropWeapon();
else if(val < top2) // change to top2
    Run();
//else if(val < top2)
//    PickWeapon();
else
    Jump();

前两种情况可以简化为:

&#13;
&#13;
function Permut(bottom2, bottom1, middle, top1, top2, val) {
    if(val <= bottom2)
        console.log('Crawl();')
    else if(val <= bottom1)
        console.log('DropWeapon();')
    else if(top1 !== null && val < top1 ||
            top1 === null && val < top2)
        console.log('Run();')
    else if(val < top2)
        console.log('PickWeapon();')
    else
        console.log('Jump();')
}

Permut(5, 10, 15, 20,   25, 21)
Permut(5, 10, 15, null, 25, 21)
&#13;
&#13;
&#13;

如果我们想要处理top1和top2都可以为空,我们可以写:

&#13;
&#13;
function Permut(bottom2, bottom1, middle, top1, top2, val) {
    if(val <= bottom2)
        console.log('Crawl();')
    else if(val <= bottom1)
        console.log('DropWeapon();')
    // here we check top1 as upper bound if it is not null
    // top2 as upper bound if top1 is null
    // no upper bound if both are null
    else if(top1 !== null && val < top1 || 
            top1 === null && (top2 === null || val < top2))
        console.log('Run();')
    // upper bound is top2 if it is not null
    // else there is no upper bound
    else if(top2 === null || val < top2)
        console.log('PickWeapon();')
    else
        console.log('Jump();')
}

Permut(5, 10, 15, 20,   25, 21)
// top1 null means we can't PickWeapon
Permut(5, 10, 15, null, 25, 21)
Permut(5, 10, 15, null, 25, 27)
// top2 null means we can't Jump
Permut(5, 10, 15, 20, null, 27)
// both top1 and top2 null means we can neither PickWeapon nor Jump
Permut(5, 10, 15, null, null, 27)
&#13;
&#13;
&#13;

然后你所要做的就是处理:

// bottom1 null means we can't DropWeapon
// bottom2 null means we can't Crawl

对于代码覆盖率,以下代码段将为您提供所有32种组合:

&#13;
&#13;
var bo2="B2", bo1="B1", mid="MI", to1="T1", to2="T2";
var b2, b1, mi, t1, t2;

var empty = "__";

for(var i=0;i<2;i++) {
    b2 = i==0?bo2:empty;
    for(var j=0;j<2;j++) {
        b1 = j==0?bo1:empty ;
        for(var k=0;k<2;k++) {
            mi = k==0?mid:empty ;
            for(var l=0;l<2;l++) {
                t1 = l==0?to1:empty;
                for(var m=0;m<2;m++) {
                    t2 = m==0?to2:empty;
                    
                    console.log(b2+"."+b1+"."+mi+"."+t1+"."+t2);
                }
            }
        }
    }
}
&#13;
&#13;
&#13;

然后,您可以将第一行替换为实际值,例如2,4,6,8,10,并将"__"替换为null,以便获得除val之外的所有可能输入。
然后,您可以添加console.log(...)val1的循环,而不是调用11,这样您就可以获得所有情况(包括边缘情况)。
这意味着您可以获得32*11 = 352次迭代以获得完整的覆盖率。

这是C# example