-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoly.py
More file actions
246 lines (173 loc) · 7.25 KB
/
Poly.py
File metadata and controls
246 lines (173 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File: Poly.py
# Description:
# Student Name: Sashi Ayyalasomayajula
# Student UT EID: sa55465
# Partner Name: Alexander Pinnarwan
# Partner UT EID: acp3576
# Course Name: CS 313E
# Unique Number:
# Date Created:
# Date Last Modified:
import sys
class Link (object):
def __init__ (self, coeff = 1, exp = 1, next = None):
self.coeff = coeff
self.exp = exp
self.next = next
def __str__ (self):
return '(' + str (self.coeff) + ', ' + str (self.exp) + ')'
class LinkedList (object):
def __init__ (self):
self.first = None
def setfirst(self):
self.first = Link(3,5)
self.insert_in_order(2,6)
#returns true if a term with a similar exponent already exists within the polynomial
def exp_in_list(self,exp):
current = self.first
yes = False
while current!=None:
if current.exp==exp:
yes = True
current = current.next
return yes
# keep Links in descending order of exponents
def insert_in_order (self, coeff, exp):
current = self.first
newLink = Link(coeff,exp)
if current==None: #if empty, new link is first
self.first = newLink
elif current.next==None: #if only one element in list, see if exp of newlink is greater or equal or less, then set accordingly
if current.exp<newLink.exp:
self.first = newLink
newLink.next = current
elif current.exp==newLink.exp:
current.coeff+=int(newLink.coeff)
else:
current.next = newLink
else: #if more than 2 elements in list, go through until you find an exponent less than or equal to newlink.exp and set accordingly
previous = self.first
inserted = False
if self.exp_in_list(exp): #method used to see if there already exists an element with the same exp. If true, find element and add coefficients
while current!=None:
if current.exp==newLink.exp:
current.coeff+=int(newLink.coeff)
break
current = current.next
else:
while(current!=None):
if current.exp<newLink.exp:
if current!=self.first:
previous.next = newLink
newLink.next = current
else:
self.first = newLink
newLink.next = current
inserted = True
break
elif current.exp==newLink.exp: #adding coefficients for same exp
current.coeff+=int(newLink.coeff)
break
previous = current
current = current.next
if not inserted: #if no element has an exponent less than newlinks, and newlinks exp is already not in the list, add newlink at the end
previous.next = newLink
# get number of links
def get_num_links(self):
num = 0
curr = self.first
while (curr != None):
if curr.coeff!=0:
num += 1
curr = curr.next
return num
# add polynomial p to this polynomial and return the sum
def add (self, p):
sum_poly = LinkedList()
selfcurr = self.first
pcurr = p.first
while (selfcurr!=None) and (pcurr!=None): #while both lists are currently on a non -None element
if selfcurr.exp==pcurr.exp: #if the exps are equal, add coefficients
if selfcurr.coeff+pcurr.coeff!=0: #if the coefficients cancell, move on to next element
sum_poly.insert_in_order(int(selfcurr.coeff)+int(pcurr.coeff),selfcurr.exp)
selfcurr = selfcurr.next
pcurr = pcurr.next
continue
else:
selfcurr = selfcurr.next
pcurr = pcurr.next
continue
elif selfcurr.exp>pcurr.exp: #if self exp is greater, add to list and only move self to next list and keep pcurr
sum_poly.insert_in_order(selfcurr.coeff,selfcurr.exp)
selfcurr = selfcurr.next
continue
elif pcurr.exp>selfcurr.exp: #viceversa if pcurr exp is greater
sum_poly.insert_in_order(pcurr.coeff,pcurr.exp)
pcurr = pcurr.next
continue
if (selfcurr==None) and (pcurr!=None): #if one of the lists have reached the end, append the rest of the other list to the sum list.
while pcurr!=None:
sum_poly.insert_in_order(pcurr.coeff,pcurr.exp)
pcurr = pcurr.next
if (pcurr==None) and (selfcurr!=None):
while selfcurr!=None:
sum_poly.insert_in_order(selfcurr.coeff,selfcurr.exp)
selfcurr = selfcurr.next
return sum_poly
# multiply polynomial p to this polynomial and return the product
def mult (self, p):
mult_L = LinkedList()
pcurr = p.first
self_first = self.first
selfcurr = self.first
while pcurr!=None: #go through all elements of pcurr
selfcurr = self_first
while selfcurr!=None: #for each element in pcurr, multiply and append the result for each element in self
mult_L.insert_in_order(pcurr.coeff*selfcurr.coeff,pcurr.exp+selfcurr.exp)
selfcurr = selfcurr.next #move on to next element in self
pcurr = pcurr.next #move on to next element in p
return mult_L
# create a string representation of the polynomial
def __str__ (self):
s = ''
current = self.first
if current==None: #if list is empty print ''
return s
else:
while current!=None:
if current.coeff==0: #dont print the element if the coefficient is zero
current = current.next
continue
if (current.next!=None): #if not last element, add the +
s+='('+str(current.coeff)+', '+str(current.exp)+') + '
else:
s+='('+str(current.coeff)+', '+str(current.exp)+')'
current = current.next
return s
def main():
# read data from file poly.in from stdin
f = sys.stdin.readlines()
#f = open('/Users/sashi_ayyalasomayajula/Downloads/CS313E Programs/poly.in','r').readlines()
N_1 = int(f[0].strip())
N_2 = int(f[N_1+2])
index_of_q = N_1+3
P = LinkedList()
Q = LinkedList()
# create polynomial p
for i in range (1,N_1+1):
line = f[i].strip().split(' ')
coeffP,expP = int(line[0]),int(line[1])
P.insert_in_order(coeffP,expP)
# create polynomial q
for j in range (index_of_q,index_of_q+N_2):
line = f[j].strip().split()
coeffQ,expQ = int(line[0]),int(line[1])
Q.insert_in_order(coeffQ,expQ)
# get sum of p and q and print sum
#print('P',P)
#print('Q',Q)
print(P.add(Q))
# get product of p and q and print product
print(P.mult(Q))
if __name__ == "__main__":
main()