#poly_repr.py exercise 6.10 #The two functions are copied from the book, section 6.1.3 def eval_poly_dict(poly, x): sum = 0.0 for power in poly: sum += poly[power]*x**power return sum def eval_poly_list(poly, x): sum = 0 for power in range(len(poly)): sum += poly[power]*x**power return sum #-0.5+2*x**100, dict version poly_dict = {0:-0.5,100:2} #same polynomial as a list poly_list = [0]*101 poly_list[0]= -0.5 poly_list[100] = 2 #print both to the screen print(poly_dict) print(poly_list) #evaluate and print both for x = 1.05 print(eval_poly_dict(poly_dict,1.05)) print(eval_poly_list(poly_list,1.05)) """ Terminal> python poly_repr.py {0: -0.5, 100: 2} [-0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2] 262.50251569260803 262.50251569260803 """