a2solutions

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

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

View Full Document Right Arrow Icon
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 coefficient is non-zero and there are n + 1 nodes. Therefore the runtime is Θ( n ) in the worst-case. 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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):
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online