#๐ polynomial addition
102 messages ยท Page 1 of 1 (latest)
@rich cypress
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
dont ask to ask , just ask your question and someone who knows the answer will get back to you
this is above me
bruh i dont even know why the underscores are there def aa_
;-;
u dont need to worry thats a class
wait holdup
the variables are diff self.degree should be == self.order
what does doesn't work mean? what's the expected result and the actual result?
this is another file with unit tests
it doesnt pass the unit tests ie the addition of poly1+p2
giving the code that "doesnt work" an input
the unit test file is flawless and i can reverse engineer it kinda but im at a dead end
otherwise i wouldnt be here tbh
this is a bit of a lame unit tester thing, it blocks the original error message and shows you an uninformative message instead
i know
i wish it wouldnt work this way
but it's basically how it's graded
but its to do with the code
@real steppelemme post the pic , check nd say @trail drift
dont post your question in someone else's thread
create your own thread (read #โ๏ฝhow-to-get-help on how to create a thread like this oen )
so you have 2 options to work with your code more efficiently:
- copy your code into a file and run it yourself without the crappy unit tester, your error messages will be readable then
- if you can edit the unit tester, at least temporarily, you can add these lines inside the
except:block to print the error:
import traceback
traceback.print_exc()```
how ?
ah wait
@real steppe@trail drift
yes but still
idk what doesnt work
so what are you having trouble with exactly
this doesnt sum them correctly
it'd be nice if you posted the actual code instead of screenshots
!code
!paste if there's a lot
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
what order are your polynomials stored in? highest degree to smallest, or smallest to highest?
e.g. if i want ax + b, do you store them as a, b or as b, a?
if you append zeros, this adds zeros at the end on the right
this works if you write your polys in ascending powers only
b + ax, c + bx + ax^2, etc
#1227608240670375997 if anyone free help ya
go away
for real, go away
ok sorry

at a glance your code looks fine except for one thing
you don't check if the result has 0s at the end
it is done in part A
idk what part A is
you can just share the code here
yea
def add(self, other):
'''Method to find the sum of two polynomials'''
Initialself = self.order #take note of current order
Initialother = other.order #take note of current order
if self.order < other.order: #checks which polynomial is of higher order
while self.order < other.order: #as long as one polynomial is of higher order, append a 0
self.coefficients.append(0)
self.order += 1
elif self.order > other.order: #checks which polynomial is of higher order
while self.order > other.order: #as long as one polynomial is of higher order, append a 0
other.coefficients.append(0)
other.order += 1
NewCoefficients = [] #define a new list to represent the sum of the polynomials
for doinkzoink in range(0, 1+self.order): #go through every term and add terms of the same degree together
NewCoefficients.append((self.coefficients[doinkzoink])+other.coefficients[doinkzoink]) #append the new coefficient to the new list
self.order = Initialself #reset the order to the initial one
other.order = Initialother #reset the order to the initial one
return polynomial(NewCoefficients)
Hey @rich cypress!
It looks like you're trying to paste code into this channel.
Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
;-;
read the bot's message
"py def add(self, other):
'''Method to find the sum of two polynomials'''
Initialself = self.order #take note of current order
Initialother = other.order #take note of current order
if self.order < other.order: #checks which polynomial is of higher order
while self.order < other.order: #as long as one polynomial is of higher order, append a 0
self.coefficients.append(0)
self.order += 1
elif self.order > other.order: #checks which polynomial is of higher order
while self.order > other.order: #as long as one polynomial is of higher order, append a 0
other.coefficients.append(0)
other.order += 1
NewCoefficients = [] #define a new list to represent the sum of the polynomials
for doinkzoink in range(0, 1+self.order): #go through every term and add terms of the same degree together
NewCoefficients.append((self.coefficients[doinkzoink])+other.coefficients[doinkzoink]) #append the new coefficient to the new list
self.order = Initialself #reset the order to the initial one
other.order = Initialother #reset the order to the initial one
return polynomial(NewCoefficients) ""
idk how to lol
you can literally copy paste what the bot wrote
print('Hello, world!')
you used the wrong apostrophe
copy paste it from what the bot wrote
ok nmvm
๐ฟ
def __add__(self, other):
'''Method to find the sum of two polynomials'''
Initialself = self.order #take note of current order
Initialother = other.order #take note of current order
if self.order < other.order: #checks which polynomial is of higher order
while self.order < other.order: #as long as one polynomial is of higher order, append a 0
self.coefficients.append(0)
self.order += 1
elif self.order > other.order: #checks which polynomial is of higher order
while self.order > other.order: #as long as one polynomial is of higher order, append a 0
other.coefficients.append(0)
other.order += 1
NewCoefficients = [] #define a new list to represent the sum of the polynomials
for doinkzoink in range(0, 1+self.order): #go through every term and add terms of the same degree together
NewCoefficients.append((self.coefficients[doinkzoink])+other.coefficients[doinkzoink]) #append the new coefficient to the new list
self.order = Initialself #reset the order to the initial one
other.order = Initialother #reset the order to the initial one
return polynomial(NewCoefficients)
ok hot
ffs
class polynomial:
'''Class to represent Polynomials as list(s) of their coefficients.'''
def __init__(self, List):
'''Constructor for the Polynomial class recieving a list of coefficients as inputs.'''
self.coefficients = List #Creates a data attribute named coefficients containing the list of coefficients
assert type(self.coefficients) == list, 'Error: No list provided.' #Checking that the input is in list format.
if len(self.coefficients) == 0: #Checking if the list is null.
self.coefficients = [0] #If list is null/empty, replace it with a list containing a single element, equal to zero.
while self.coefficients[-1] == 0 and len(self.coefficients) > 1: #Removes all zero elements from the end of the list until the last element of the list is non-zero, or the list only contains a zero
self.coefficients.pop(-1)
for variables in self.coefficients:
assert type(variables) == int or type(variables) == float, 'Error: List contains unregognised element(s). Valid elements include integer and float values.' #Checking elements individually to ensure they valid int and float inputs.
self.degree = len(self.coefficients) - 1# Creates a data attribute named degree containing the degree of the represented polynomial, based on the provided list of coefficients, after trailing zeros removed.
if self.degree < 0 :
self.degree == 0
what?
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ polynomial addition
