A polynomial object can be created from a string that represents a polynomial as sums or differences of monomials.
Monomials are ordered from highest to lowest powers.
All factors are strictly positive, except possibly for the leading factor.
For nonzero powers, factors of 1 are only implicit.
A single space surrounds + and - signs between monomials.
See pdf for expected behaviour
# A polynomial object can be created from a string that represents a polynomial
# as sums or differences of monomials.
# - Monomials are ordered from highest to lowest powers.
# - All factors are strictly positive, except possibly for the leading factor
# - For nonzero powers, factors of 1 are only implicit.
# A single space surrounds + and - signs between monomials.
# Written by *** and Eric Martin for COMP9021
import re # split() suffices though
from collections import defaultdict
from copy import deepcopy
class Polynomial:
def __init__(self, polynomial = None):
if polynomial==None:
self.val={0:0}
else:
dic=defaultdict(int)
#print(polynomial,'p')
s1=re.sub('-','+-',polynomial)
#print(s1,'s1')
s=re.sub(' ','',s1)
#ss=re.sub('x','x^1',s)
L=s.split('+')
#print(L,'l')
key_new=0
value_new=0
#print(L,'L')
for i in L:
s=re.sub('-x','-1x',i)
s=re.search('(.*)x\^(.*)',s)
#print(s,'!')
if s==None and 'x' in i:
ss=re.sub('-x','-1x',i)
ss=re.sub('x','x^1',ss)
s=re.search('(.*)x\^(.*)',ss)
#print(s,'s')
if s==None:
key_new=0#->x^0
#print(i,'i')
if i=='':
value_new=0
else:
value_new=int(i)
elif s[1]=='':
key_new=int(s[2])
value_new=1
else:
#print('s1',s[1])
key_new=int(s[2])
value_new=int(s[1])
dic.update({key_new:value_new})
self.val=dic
# REPLACE pass ABOVE WITH YOUR CODE
#print(self.val)
def __str__(self):
s=''
for key,value in self.val.items():
if value==0:
continue
if key==0:
s+=f'{value} + '
elif key==1:
if value==1:
s+='x + '
elif value==-1:
s+='-x + '
else:
s+=f'{value}x + '
elif value==1:
s+=f'x^{key} + '
elif value==-1:
s+=f'-x^{key} + '
else:
s+=f'{value}x^{key} + '
if s=='':
s='0'
elif s[-2]=='+':
s=s[:-3]
st=re.sub('\+ -','- ',s)
if self.val=={}:
st=0
return st
def __add__(self,other):
dic=self.val.copy()
dic2=defaultdict(int)
for key,value in other.val.items():
dic[key]=self.val[key]+other.val[key]
s=''
#print(dic,'dic')
if len(dic)>1:
for k in sorted(dic.keys(),reverse=True):
dic2.update({k:dic[k]})
elif len(dic)==1:
dic2=dic.copy()
else:
st=0
for key,value in dic2.items():
if value==0:
continue
if key==0:
s+=f'{value} + '
elif key==1:
if value==1:
s+='x + '
elif value==-1:
s+='-x + '
else:
s+=f'{value}x + '
elif value==1:
s+=f'x^{key} + '
elif value==-1:
s+=f'-x^{key} + '
else:
s+=f'{value}x^{key} + '
if s=='':
s='0'
elif s[-2]=='+':
s=s[:-3]
st=re.sub('\+ -','- ',s)
return Polynomial(st)
def __iadd__(self,other):
dic=self.val.copy()
for key,value in other.val.items():
dic[key]=self.val[key]+other.val[key]
s=''
if len(dic)>1:
dic2={}
for k in sorted(dic.keys(),reverse=True):
dic2.update({k:dic[k]})
self.val=dic2.copy()
elif len(dic)==1:
self.val=dic.copy()
else:
st=0
for key,value in self.val.items():
if value==0:
continue
if key==0:
s+=f'{value} + '
elif key==1:
if value==1:
s+='x + '
elif value==-1:
s+='-x + '
else:
s+=f'{value}x + '
elif value==1:
s+=f'x^{key} + '
elif value==-1:
s+=f'-x^{key} + '
else:
s+=f'{value}x^{key} + '
if s=='':
s='0'
elif s[-2]=='+':
s=s[:-3]
st=re.sub('\+ -','- ',s)
return Polynomial(st)
def __mul__(self,other):
dic=defaultdict(int)
dic2=defaultdict(int)
for key,value in other.val.items():
for key2,value2 in self.val.items():
dic[key+key2]+=value*value2
s=''
if len(dic)>1:
for k in sorted(dic.keys(),reverse=True):
dic2.update({k:dic[k]})
elif len(dic)==1:
dic2=dic.copy()
else:
st=0
for key,value in dic2.items():
if value==0:
continue
if key==0:
s+=f'{value} + '
elif key==1:
if value==1:
s+='x + '
elif value==-1:
s+='-x + '
else:
s+=f'{value}x + '
elif value==1:
s+=f'x^{key} + '
elif value==-1:
s+=f'-x^{key} + '
else:
s+=f'{value}x^{key} + '
if s=='':
s='0'
elif s[-2]=='+':
s=s[:-3]
st=re.sub('\+ -','- ',s)
return Polynomial(st)
def __imul__(self,other):
dic=defaultdict(int)
for key,value in other.val.items():
for key2,value2 in self.val.items():
dic[key+key2]+=value*value2
s=''
if len(dic)>1:
dic2={}
for k in sorted(dic.keys(),reverse=True):
dic2.update({k:dic[k]})
self.val=dic2.copy()
elif len(dic)==1:
self.val=dic.copy()
else:
st=0
for key,value in self.val.items():
if value==0:
continue
if key==0:
s+=f'{value} + '
elif key==1:
if value==1:
s+='x + '
elif value==-1:
s+='-x + '
else:
s+=f'{value}x + '
elif value==1:
s+=f'x^{key} + '
elif value==-1:
s+=f'-x^{key} + '
else:
s+=f'{value}x^{key} + '
if s=='':
s='0'
elif s[-2]=='+':
s=s[:-3]
st=re.sub('\+ -','- ',s)
return Polynomial(st)
# REPLACE pass ABOVE WITH YOUR CODE
# DEFINE OTHER METHODS
# p = Polynomial('2')
# print('***')
# print(p + Polynomial('2'))
# p += Polynomial('2')
# print('***')
# print(p)
# p = Polynomial('3')
# print('***')
# print(p + Polynomial('-3'))
# print('***')
# print(p)
# p += Polynomial('-3')
# print('***')
# print(p)
# p = Polynomial('x')
# print('***')
# print(p + Polynomial('2x^3 + x - 4'))
# print('***')
# print(p)
# print('***')
# p += Polynomial('2x^3 + x - 4')
# print(p)
# print('***')
# p = Polynomial('4x^5 - x^2 + 6')
# print(p * Polynomial('x^3 - x + 4'))
# print(p)
# print('***')
# p *= Polynomial('x^3 - x + 4')
# print(p)
# print('***')
# p = Polynomial('x^4 - x^3 + x^2 - x')
# print(p * Polynomial('-2x^3 + 3x^2 - 4x + 5'))
# print('***')
# p = Polynomial()
# print(p + Polynomial('2'))
# #2
# print(p)
# #0
# p += Polynomial('2')
# print(p)
# #2
我有个0次方的排序存在问题有些测试0次的就被排序在了首位!是个很容易可以de的bug,哭泣一分钟!
网友评论