#๐Ÿ”’ polynomial addition

102 messages ยท Page 1 of 1 (latest)

rich cypress
#

code decipher

burnt fulcrumBOT
#

@rich cypress

Python help channel opened

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.

rich cypress
#

can anyone help with this?

#

will add pics and am willing to screenshare preferabley

real steppe
rich cypress
#

ok so thats the code

#

that doesnt work

#

this was the first part

trail drift
#

this is above me

rich cypress
#

nah if u give it a sec

#

its not bad

#

v

trail drift
#

bruh i dont even know why the underscores are there def aa_

rich cypress
#

;-;

#

u dont need to worry thats a class

#

wait holdup

#

the variables are diff self.degree should be == self.order

thick spire
rich cypress
#

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

thick spire
# rich cypress

this is a bit of a lame unit tester thing, it blocks the original error message and shows you an uninformative message instead

rich cypress
#

i know

#

i wish it wouldnt work this way

#

but it's basically how it's graded

#

but its to do with the code

west adder
#

@real steppelemme post the pic , check nd say @trail drift

real steppe
thick spire
#

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()```
west adder
rich cypress
#

idk what doesnt work

proven grail
#

so what are you having trouble with exactly

rich cypress
drowsy forum
# rich cypress

it'd be nice if you posted the actual code instead of screenshots

#

!code

burnt fulcrumBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

drowsy forum
#

!paste if there's a lot

burnt fulcrumBOT
#
Pasting large amounts of code

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.

proven grail
#

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

rich cypress
#

self.order/self.degree = the degree of the polynomial

west adder
#

#1227608240670375997 if anyone free help ya

rich cypress
west adder
rich cypress
proven grail
west adder
rich cypress
proven grail
#

you don't check if the result has 0s at the end

rich cypress
#

it is done in part A

proven grail
#

idk what part A is

rich cypress
#

nvm, kinda need someone on vc

#

so i can screenshare tbh

proven grail
#

you can just share the code here

rich cypress
#

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)
burnt fulcrumBOT
#

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.
rich cypress
#

;-;

proven grail
#

read the bot's message

rich cypress
#

"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

proven grail
#

you can literally copy paste what the bot wrote

rich cypress
#

print('Hello, world!')

proven grail
#

you used the wrong apostrophe

rich cypress
#

ok

#

uhh

#

oh yh

#

'' py dwaoiuhedwa ''

#

idk how ytf

#
print('Hello, world!')
proven grail
#

copy paste it from what the bot wrote

rich cypress
#

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

proven grail
#

this we already had in the image

#

what was this "part A" you said

rich cypress
#

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
proven grail
#

looks all fine at a glance

#

did you try some examples yourself?

rich cypress
#

yea its just techy

#

and im reaching with how much ik

proven grail
#

what?

burnt fulcrumBOT
#
Python help channel closed

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