Skip to content

Commit 8f22e79

Browse files
committed
unit4 pro6 solved
1 parent b6af3a6 commit 8f22e79

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

unit4/pro4.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
'''
2+
Devise and implement an algorithm to multiply two polynomials
3+
'''
4+
15
from opus7.polynomialAsSortedList import PolynomialAsSortedList
26

37
class MultiplyPolynomial(PolynomialAsSortedList):
@@ -21,6 +25,7 @@ def __mul__(self, poly):
2125

2226
return result
2327

28+
2429
# test (1+2x+x^2)*(1+x)
2530
m1 = MultiplyPolynomial()
2631
m1.addTerm(MultiplyPolynomial.Term(1,0))

unit4/pro5.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Devise and implement an algorithm to compute the power of a polynomial, where k is a positive integer. What is the running time of your algorithm?
3+
'''
4+
5+
from pro4 import MultiplyPolynomial
6+
7+
class PowerPolynomial(MultiplyPolynomial):
8+
def __mul__(self, poly):
9+
return MultiplyPolynomial.__mul__(self, poly)
10+
11+
def expo(self, ex):
12+
result = self
13+
print type(result)
14+
print type(self)
15+
for i in range(ex-1):
16+
result = result * self
17+
18+
return result
19+
20+
def powerPoly(poly, exp):
21+
if exp == 0:
22+
result = MultiplyPolynomial()
23+
result.addTerm(MultiplyPolynomial.Term(1,0))
24+
return result
25+
result = poly
26+
for i in range(exp-1):
27+
result *= poly
28+
29+
return result
30+
31+
def pro5():
32+
print "===pro5==="
33+
m = MultiplyPolynomial()
34+
m.addTerm(MultiplyPolynomial.Term(1,0))
35+
m.addTerm(MultiplyPolynomial.Term(1,1))
36+
print powerPoly(m, 3)
37+
38+
pro5()

unit4/pro6.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'''
2+
For some calculations it is necessary to have very large integers, i.e., integers with an arbitrarily large number of digits. We can represent such integers using lists. Design and implement a class for representing arbitrarily large integers. Your implementation should include operations to add, subtract, and multiply such integers, and to compute the power of such an integer, where k is a small positive integer. Hint: Base your design on the Polynomial class given in Program .
3+
'''
4+
5+
from opus7.polynomialAsOrderedList import PolynomialAsOrderedList
6+
from opus7.visitor import Visitor
7+
from opus7.linkedList import LinkedList
8+
9+
class LargeInt(PolynomialAsOrderedList):
10+
11+
def __add__(self, poly):
12+
result = LargeInt()
13+
p1 = iter(self._list)
14+
p2 = iter(poly._list)
15+
term1 = self.nextTerm(p1)
16+
term2 = self.nextTerm(p2)
17+
carry = 0
18+
while term1 is not None and term2 is not None:
19+
temp = term1 + term2 + carry
20+
carry = temp/10
21+
temp %= 10
22+
print "adding %d to %d, get %d and %d as carry" % (term1, term2, temp, carry)
23+
result.addTerm(temp)
24+
term1 = self.nextTerm(p1)
25+
term2 = self.nextTerm(p2)
26+
while term1 is not None:
27+
print "entering only term1"
28+
term1 += carry
29+
carry = term1/10
30+
term1 %= 10
31+
print "adding %d and %d as carry" % (term1, carry)
32+
result.addTerm(term1)
33+
term1 = self.nextTerm(p1)
34+
while term2 is not None:
35+
print "entering only term2"
36+
term2 += carry
37+
carry = term2/10
38+
term2 %= 10
39+
print "adding %d and %d as carry" % (term2, carry)
40+
result.addTerm(term2)
41+
term2 = self.nextTerm(p2)
42+
if carry != 0:
43+
print "adding extra carry", carry
44+
result.addTerm(carry)
45+
return result
46+
47+
def nextTerm(self, iter):
48+
"""
49+
(PolynomialAsSortedList, Iterator) -> Polynomial.Term
50+
Returns the next term or None if there are no more terms.
51+
"""
52+
try:
53+
return iter.next()
54+
except StopIteration:
55+
return None
56+
57+
def generateInt(self, *ints):
58+
for i in reversed(ints):
59+
self.addTerm(i)
60+
61+
def __str__(self):
62+
r = self.ReverseVisitor()
63+
self.accept(r)
64+
return str(r.list)
65+
66+
class ReverseVisitor(Visitor):
67+
def __init__(self):
68+
self._list = LinkedList()
69+
70+
def visit(self, obj):
71+
self._list.prepend(obj)
72+
73+
def getList(self):
74+
return self._list
75+
76+
list = property(
77+
fget = lambda self: self.getList())
78+
79+
def pro6():
80+
p1 = LargeInt()
81+
p1.generateInt(1, 9, 9)
82+
p2 = LargeInt()
83+
p2.generateInt(9,9)
84+
result = p1 + p2
85+
print p1, " + ", p2, " = ", result
86+
87+
pro6()

0 commit comments

Comments
 (0)