• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

145
Views
¿Cómo deshacerse de la complejidad en el algoritmo?

Ejercicio: Escriba una función múltiple (a, b) que multiplique el número a por el número b sin usar el operador "*" o el método Math.imul.

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

Código:

 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); }

Ok, escribo este código y pasa las pruebas. Pero eslint se queja de la función de complejidad excesiva: Function 'multiple' has a complexity of 15. Maximum allowed is 10. eslint (complexity)

¿Cómo reducir la complejidad de mi función?

ACTUALIZAR

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

Sí, realmente funcionó, pero si miro mi código, donde puedo acortar operaciones repetidas, me veo ciego, pero quiero entenderlo.

ACTUALIZAR 2

La solución debe pasar todas las pruebas:

 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) })
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede reducir la cantidad de pruebas en las que prueba el letrero. En su lugar, podría traducir la llamada con un número negativo a una llamada con un número positivo y negar el resultado.

Aquí hay una versión más compacta.

NB: Supongo que el espíritu del ejercicio era no usar / tampoco, ya que de lo contrario es trivial usar el hecho de que 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");

almost 3 years ago · Juan Pablo Isaza Report

0

Puede usar la identidad |a*b|=exp(ln(|a|) + ln(|b|) o 0 iff a==0 o b==0.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error