Multiplying Polynomials
Description:
SITUATION
One day, as you wake up, you have a terrible realization:
You have magically time-traveled, going all the way back to high school.
Ignoring the weird scientific implications of this event, you focus on what's truly important: HOMEWORK
You see, your teacher has handed out hours' worth of math assignments for you. However, you have better things to do, such as trying to figure out how you even ended up in this situation.
Using your retained knowledge, in order to save time, you decide to create a program to do your homework for you, even if it takes way longer than just being a normal person and doing it yourself.
TASK
Write a function / method called polynomial_product()
, which takes two strings representing polynomials as input and returns another string representing the product of those polynomials.
INPUT
You will be given two strings of the form: ax^n + bx^n-1 + cx^n-2 ... + hx^2 + ix + j
.
Where a, b, c, ..., i, j
and n
are integers.
For example: 4x^3 - x^2 + 7x - 1
and x^2 - 2x + 1
Special Cases:
- The variable won't always be x; it may be u, v, A, or any other ascii letter (character code: 65 to 90 and 97 to 122). However, the variable is always consistent between the polynomials.
You might getu^2 - 1
and2u + 1
as well asA^3 - A
and2A^2 + 8
, but never3x^2 - x
and2y + 1
. - If the coefficient of a term is
1
, then it will simply not show up, unless it's the constant term.
For example,u^3 - u^2 + u - 1
oru^2 + u + 1
- If the coefficient of a term is
0
, then, the whole term will, simply, not show up.
For example,2H^2 + 1
or3H^3 - 4H
OUTPUT
Return a string representing a polynomial of the form: ax^n + bx^n-1 + cx^n-2 ... + hx^2 + ix + j
.
For example, given x^2 + 2x + 1
and x + 1
, return x^3+3x^2+3x+1
Rules for formatting the answer:
- Your answer should not contain whitespace; all the terms, must, be together.
For example,s^2+2s+1
instead ofs^2 + 2s + 1
- When the coefficient of a term is
1
or-1
, it shouldn't show up, unless it's the constant term.
For example,-x^2+x-1
instead of-1x^2+1x-1
- When the coefficient of a term is
0
, the whole term shouldn't appear in the answer.
For example,2Y^3+Y
instead of2Y^3+0Y^2+Y+0
- The terms should be in order from the highest degree to the lowest, from left to right.
For example,a^4+2a^2+1
instead of1+2a^2+a^4
- The variable used for the answer should be the same as the input; don't go turning every variable into x's.
For example, givenb^2 + 1
and9b - 2
, return9b^3-2b^2+9b-2
rather than9x^3-2x^2+9x-2
.
EXAMPLES
polynomial_product("u^2 + 2u+1", "u + 1") -> "u^3+3u^2+3u+1"
polynomial_product("x^2", "3x - 1") -> "3x^3-x^2"
polynomial_product("2", "4y - 4") -> "8y-8"
polynomial_product("-4r^2 + 1", "-1") -> "4r^2 - 1"
polynomial_product("1", "p^3") -> "p^3"
polynomial_product("1", "-1") -> "-1"
polynomial_product("0", "2 - x") -> "0"
polynomial_product("-1", "0") -> "0"
polynomial_product("v^2 - 1+3v^3", "1+v^2") -> "3v^5+v^4+3v^3-1"
RANDOM TESTS
0 <= exponents <= 4500
-85000 <= coefficients <= 85000
20
small tests
30
medium tests
20
big tests
5
really big tests
This kata isn't focused on performance. However, don't go writing spaghetti code just because of this; you can still timeout if your code is poorly written.
Similar Kata:
Stats:
Created | Jan 12, 2023 |
Published | Jan 13, 2023 |
Warriors Trained | 936 |
Total Skips | 97 |
Total Code Submissions | 1884 |
Total Times Completed | 173 |
Python Completions | 111 |
Rust Completions | 16 |
PHP Completions | 5 |
JavaScript Completions | 48 |
Total Stars | 73 |
% of votes with a positive feedback rating | 92% of 53 |
Total "Very Satisfied" Votes | 47 |
Total "Somewhat Satisfied" Votes | 4 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 12 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |