This preview shows pages 1–3. Sign up to view the full content.
CS 234 Fall 2010
Assignment 2 Written Component Solutions
Express the worstcase runtime of the following three code fragments using
Θ
notation in terms
of
n
. Informally justify your answers.
1.
P.evaluate(0)
Assume
evaluate
(below) is in
polynomial
.
py
and
P
is a
polynomial
of degree
n
.
## evaluate: polynomial int > int
## Consumes a polynomial self and integer m and produces the
## result of evaluating self at m (i.e. substituting m into self).
## Does not change self.
def evaluate(self, m):
result = 0
cur = self.term
while cur is not None:
result = result + cur.coeff
*
(m
**
cur.exp)
cur = cur.next
return result
Each line of code is
Θ(1)
in isolation so the runtime is determined by the number of loop
iterations. The loop is entered once for each node. In the worstcase, every coefﬁcient is
nonzero and there are
n
+ 1
nodes. Therefore the runtime is
Θ(
n
)
in the worstcase.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document2.
import math
R = range(1 , round(math.sqrt(n))
for i in R:
print "Phil Kessel will score 50 goals."
R = range(0 , n , n/4):
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '09
 BARANOSKI

Click to edit the document details