a2solutions

a2solutions - CS 234 Fall 2010 Assignment 2 Written...

This preview shows pages 1–3. Sign up to view the full content.

CS 234 Fall 2010 Assignment 2 Written Component Solutions Express the worst-case 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 worst-case, every coefﬁcient is non-zero and there are n + 1 nodes. Therefore the runtime is Θ( n ) in the worst-case. 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2. 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.

{[ snackBarMessage ]}

Page1 / 3

a2solutions - CS 234 Fall 2010 Assignment 2 Written...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online