4 kyu

Multiplying Polynomials

118 of 183Yushi.py
Description
Loading description...
Mathematics
Strings
Regular Expressions
  • Please sign in or sign up to leave a comment.
  • ahmet_popaj Avatar

    Great math kata.

  • Kippekont Avatar

    The random tests for PHP often expect an incorrect or sometimes empty result:

    Testing: 5U^3+7U^2+U and U^2+9U^3-7U-3 Failed asserting that two strings are identical. Expected: '45U^6+68U^5-19U^4-63U^3-28U^2-3U' (My result) Actual : '-12' (Expected by test)

    Also the RandomRealyBigPolynomials feel impossible in PHP, the memory limit is very quickly reached.

  • RileyHunter Avatar

    Bugfixing fork to address cases in which the JS reference solution breaks down:

    https://www.codewars.com/kumite/65383e33e681c41613bcb605?sel=65383e33e681c41613bcb605

    Details on bugfix here:

    https://www.codewars.com/kata/63c05c1aeffe877458a15994/discuss/javascript#6536cc0c49d3ef23dade29b1

    Waiting on approval.

  • ThatOneGuyWhoCodes Avatar

    Are there any Javascript libraries that could be helpful for this? I haven't worked with JS imports before and have no idea if there are any math based one.

    If not, is there a way I can get my code from O(n^2) to O(nlogn)?

    Also I'm trying to learn complexity so am I right in that assement above?

  • Olegeant Avatar

    JS random tests sometimes fail with '-NaN' generated by test as a correct answer.
    Some logs given below, my solutions are obviously correct (which can be checked with direct calculation):

    • Testing: -1+4a and a-9a^3: expected '-36a^4+9a^3+4a^2-a' to equal '-NaN'
    • Testing: -8+T and T+5T^3+T^4: expected 'T^5-3T^4-40T^3+T^2-8T' to equal '-NaN'
    • Testing: -1+4Z and Z^3: expected '4Z^4-Z^3' to equal '-NaN'
    • Testing: -2x and x^2: expected '-2x^3' to equal '-NaN'
  • Daos711 Avatar

    Maybe reduce the number of tests? I pass 129 tests and time out. I think my code is quite good

  • Mednoob Avatar

    JS: chai.config.truncateThreshold should be set to 0 or false. User can't see the error of their result in big random tests.

  • cwps Avatar
  • jayyyyyy Avatar

    I forgot the formula for polynomial multiplication and thought you only have to multiply numbers with same powers D:

  • akar-0 Avatar
  • alexc19 Avatar

    Very fun, I barely made the timeout cut with no optimizations and no libraries. I thought my solution was ugly but in the end I'm quite happy.

  • avermakov Avatar

    I think this kata needs the Performance tag. The inputs are enormous.

  • seniorCrutchDeveloper Avatar

    Nice kata. But maybe we should ban numpy, because it makes the problem trivial. Without using numpy it is necessary to implement an algorithm that does not do any extra calculations otherwise you will get a timeout. I had to optimize the calculations several times to get it to work in the end.

  • Blind4Basics Avatar

    Hi,

    the kata needs some improvment. But I lined it, overall.

    • missing sample tests:
      • result is 0
      • a test with the monom of degree 0 equal to -1
      • a test with the monom of degree 0 equal to ...1
      • a test with some other monom with a coefficient of ...1
    • I think most of those alreayd appears in the fixed tests, but if not they whousl be added there as well
    • the random tests generation is way below efficient: list.pop(random index) makes the building in a loop O(N²). Prefer the use of the dedicated tools, lile:
      • random.choices(range(max_exponent), k=random.randrange(max_exponent)) to generate unique exponents (note: nothing forbids to have multiple times the same exponent. In both cases, the behavior should be talked about in the description (I don't remember it, maybe I missed it))
      • there is absolutely no reason the générate unique coefficients. Considering the ranges used, the chances of duplicates are nuts anyway... => random.randint(-max_coeficient, max_coeficient) will do a perfect job
      • on the other hand, the random generator must use special branches of logic to make far more current the edge cases, especially those for a result including degres 0 et 1, with various values of the coefficient (0, 1, -1, other).

    Cheers

  • dfhwze Avatar

    How many really big random tests are there? I have no clue how much to optimise my code.

  • Voile Avatar

    Reference solution handles 0 incorrectly:

    Testing: 0 and -9y: '0' should equal ''
    
  • Voile Avatar

    Typo in initial code function name: polinomial_product -> polynomial_product