我现在有个很好的案子了。我有一个依赖于2个变量的函数,我需要实现此函数的代码以正确响应给定的期望值表格
const cases = [
[1, 1, false],
[1, 2, true],
[1, 3, false],
[1, 4, true],
[1, 5, false],
[1, 6, true],
[2, 1, true],
[2, 2, false],
[2, 3, true],
[2, 4, false],
[2, 5, true],
[2, 6, false],
];
// not relevant but fyi as code is js used jest testeach to check all of cases outputs
test.each(
cases,
function(nr1, nr2, expected) {
expect(isInGroup(nr1, nr2)).toBe(expected);
},
);
以上是firstNr: int,secondNr: int,expectedFunctionOutput: bool格式的案例
函数签名如下
export function isInGroup(firstNr, secondNr) {
// TODO implement
// return correct stuff
}
PS:案例的分布确保了variable1和variable2都有50%的true
案例。
我最初尝试了一些东西,比如将它们分组分发,但没有成功,只从提供的测试用例中获得了75%的测试用例。
export function isInGroup(firstNr, secondNr) {
return (
(firstNr % 2 === 0 && secondNr % 2 === 0) ||
(firstNr % 2 !== 0 && secondNr % 2 === 0) ||
(firstNr % 2 === 0 && secondNr % 2 !== 0)
);
}
另外,由于值是数字而不是布尔值,因此不能使用布尔表直接查找公式
发布于 2021-07-15 16:09:57
这是我自己解决的。
此函数提供所需的结果:
return firstNr % 2 !== 0 ? secondNr % 2 === 0 : secondNr % 2 !== 0;
https://stackoverflow.com/questions/68385422
复制相似问题