Ich habe mein Bestes versucht, um die Potenzen und Koeffizienten eines beliebigen Polynoms zu erhalten, das der Benutzer auf einer separaten Liste angibt.Erhalten von Potenzen und Koeffizienten eines Polynoms auf separaten Listen
Grundsätzlich wird nur der Koeffiziententeil verwendet und nicht der Leistungsteil, aber die Liste der Leistungen (der Variablen wird nur zum Vergleich verwendet). Ich habe es getan und es funktioniert, aber der Code ist irgendwie schlampig und nicht elegant. Gibt es eine bessere Möglichkeit, dies zu programmieren?
Was sollte im Grunde tun ist:
Wenn die Benutzereingaben sagen: 4x3+3
es so etwas wie zurückgeben sollte:
coeffs = [4,0,0,3]
Dies ist so, dass ich das Polynom Horner-Methode zu lösen.
Hier ist der runnable Code: REPL CODE
Der Code wird mit der Testfunktion ausgeführt wird:
x = solve(function)
x.parse()
.
#!/usr/bin/python3
######################################################################
#code information
#
#
# When the user provides the input of the form
# 4x3+2x+1
# The parse method is expected to return
# A coefficient list of the provided polynomial
# in ready for use for the horner's method of solving
#######################################################################
function = "4x3+2x+1" #this is the sample input the user is expected to give
#
class solve:
def __init__(self, string):
self.function = string
self.letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z']
#######################################################################
#######################################################################
#######################################################################
#######################################################################
def parse(self):
signs = ['+', '-', '*']
for sign in signs:
self.function = self.function.replace(sign, ' ')#this is where all the
#signs are converted
#to spaces
self.function = self.function.split() #this is where all the
#list is split into terms
self.function.sort(reverse = True) #the polynomial is sorted always
#in the decreasing order
#from higher to lower order of x
coeffs = [] #list that holds all the coefficients
powers = [] #list that holds all the powers
while self.function:
term = self.function.pop(0)#for each term in the polynomial
for letter in self.letters:
#check for the alphabets in the letters(The list above)
if letter in term:
x, y = term.split(letter)
coeffs.append(int(x))#append the coefficient to the list
if y != '':
powers.append(int(y))#append the power to the list
else:
powers.append(1) #append 1 for x^1 term
else:
try:
temp = int(term) #exception occurs here
coeffs.append(temp)#append constant term after exhaution
#of all the polynomial terms
#if no constants exits
#this is not reached
#and neither the line
#directly below
powers.append(0)#only for a constant,we have power 0
break #break nonsense to append only once
except:
pass #exception passed silently
return self.check_complete(coeffs, powers)
print("The coefficients are: ", coeffs)
print("The powers are: ", powers)
#######################################################################
#######################################################################
#######################################################################
#######################################################################
def check_complete(self, coeffs, powers):
"""This function checks if the polynomial is a
complete polynomial that is if it has all the powers of x
it does this by comparing the two lists hand in hand,
that is checks the corresponding terms"""
try:
#while the function arrives here
#power and range are assumed to be of same length
factor = 0 #factor for keeping track of index below
for index in range(len(powers)):
########################################
########################################
Index = index + factor #just cleaning up
########################################
########################################
difference = powers[Index] - powers[Index+1]
while difference > 1:
factor += 1 #factor incremented to keep track
#of where to add
difference -= 1
coeffs.insert(Index+1, 0)#in the coefficient list
#insert zeros where the
#polynomial is missing a term
except:
return coeffs #pass the exception
Haben Sie Eingabebeschränkungen?Können die Koeffizienten> = 10 oder negativ sein? – konsolas
Willkommen bei StackOverflow. Bitte lesen und befolgen Sie die Buchungsrichtlinien in der Hilfe. [Minimales, vollständiges, überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) gilt hier. Wir können Ihnen nicht effektiv helfen, bis Sie Ihren MCVE-Code veröffentlicht und das Problem genau beschrieben haben. Wir sollten in der Lage sein, Ihren gesendeten Code in eine Textdatei einzufügen und das beschriebene Problem zu reproduzieren. Insbesondere sollten wir ein treibendes Hauptprogramm und Beispiele für die Ergebnisse haben, die Sie bekommen. – Prune
@konsolas, Ja, die Koeffizienten können größer sein als jede Zahl – mathmaniage