最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何摆脱算法中的复杂性?

SEO心得admin128浏览0评论
本文介绍了如何摆脱算法中的复杂性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

练习 编写一个乘数 (a, b) 函数,将数字 a 与数字 b 相乘,而不使用*"运算符或 Math.imul 方法.

Exercise: Write a multiple (a, b) function that multiplies the number a by the number b without using the "*" operator or the Math.imul method.

multiple(1, 1) // 1 multiple(1, 2) // 2 multiple(0, 0) // 0

代码:

export default function multiple(a, b) { if (a === Infinity && b === 0 || a === -Infinity && b === 0 || a === 0 && b === Infinity || a === 0 && b === -Infinity) { return NaN; } if (b === Infinity) { if (a < 0) { return -Infinity; } return Infinity; } if (b === -Infinity) { if (a < 0) { return Infinity; } return -Infinity; } if (b === 1 || b === -1) { if (a < 0 && b < 0 || a > 0 && b < 0) { return -a; } return a; } if (b < 0) { return -a + multiple(a, b + 1); } return a + multiple(a, b - 1); }

好的,我写了这段代码,它通过了测试.但是 eslint 抱怨函数复杂度过高:Function 'multiple' 的复杂度为 15.最大允许为 10.eslint(复杂度)

Ok, I write this code and it passes the tests. But eslint complains about over complexity function: Function 'multiple' has a complexity of 15. Maximum allowed is 10. eslint (complexity)

如何降低函数的复杂度?

更新

const multiple = (a, b) => a / (1 / b);

是的,它确实有效,但是如果查看我的代码,我可以缩短重复操作的时间,我看起来很盲目,但我想了解它.

Yes its really worked, but if look on my code, where I can short a repeated operations, I looks blind, but I want to understand it.

更新 2

解决方案必须经过所有测试:

Solution must goes all tests:

const random = () => Math.floor(Math.random() * 100) * (Math.random() < 0.5 ? -1 : 1) const cases = [ [0, 1], [1, 0], [1, 1], [1, 2], [0, 0], [5, 5], [5, -5], [290, -41], [-5, 5], [-5, -5], [random(), random()], [random(), random()], [random(), random()], [random(), random()], [random(), random()], [10, -Infinity], [10, Infinity], [-10, Infinity], [-10, -Infinity], [Infinity, 10], [-Infinity, -10], [Infinity, -10], [-Infinity, 10], [0, Infinity], [0, -Infinity], [Infinity, 0], [-Infinity, 0] ] cases.forEach(([a, b]) => { console.log(`\na:${a} b:${b}`) console.log('my:', multiple(a, b), 'Fact:', a * b) })

推荐答案

您可以减少测试标志的测试次数.您可以改为将带有负数的调用转换为带有正数的调用并否定结果.

You can reduce the number of tests where you test the sign. You could instead translate the call with a negative number to a call with a positive number and negate the result.

这是一个更紧凑的版本.

Here is a more compact version.

注意:我认为练习的精神是也不使用 /,否则使用 a*b === a/(1/b)

NB: I assume the spirit of the exercise was to not use / either, as otherwise it is trivial to use the fact that a*b === a/(1/b)

function multiple(a, b) { return a < b ? multiple(b, a) : b < 0 ? -multiple(a, -b) : a === Infinity ? (b ? a : NaN) : b === Infinity || !b ? b : a + multiple(a, b - 1); } // Tests: const random = () => Math.floor(Math.random() * 200) - 100; const cases = [ [0, 1], [1, 0], [1, 1], [1, 2], [0, 0], [5, 5], [5, -5], [290, -41], [-5, 5], [-5, -5], [random(), random()], [random(), random()], [random(), random()], [random(), random()], [random(), random()], [10, -Infinity], [10, Infinity], [-10, Infinity], [-10, -Infinity], [Infinity, 10], [-Infinity, -10], [Infinity, -10], [-Infinity, 10], [0, Infinity], [0, -Infinity], [Infinity, 0], [-Infinity, 0] ] cases.forEach(([a, b]) => { let result = multiple(a, b); if (!Object.is(result, a*b)) { console.log(`\na:${a} b:${b} my result: ${result}, expected: ${a*b}`); } }) console.log("all done");

发布评论

评论列表(0)

  1. 暂无评论