{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

a2solutions

# a2solutions - import re copy index_from_right string...

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

import re, copy ## index_from_right: string string -> int ## Consumes strings str and target and produces the starting ## index of the first occurrence (from the right) of target in ## str if any, and produces -1 otherwise ## (Helper function added to fix small bug in polynomial ## method __init__.) def index_from_right(str , target): for i in range(len(str)-len(target),-1,-1): if str[i:i+len(target)] == target: return i return -1 class term: """ Fields: coeff, exp and next A node of a linked list used within class polynomial. Both coeff and exp will be non-negative integers and next refers to the next node in the linked list. """ ## __init__: term int int term -> term ## Consumes a term, coefficient, exponent and next term (None ## is the default) and constructs the corresponding term. def __init__(self, coeff, exp, next = None): self.coeff = coeff self.exp = exp self.next = next ## __str__: term -> string ## Consumes a term and produces a string consisting of the ## form cx^e where c is the coefficient and e the exponent. ## When e is 0, x^ is omitted and when e is 1, x is omitted. def __str__(self): s = str(self.coeff) if self.exp > 0: s = s + "x" if self.exp > 1: s = s + "^" + str(self.exp) return s ## is_positive: term -> bool ## Consumes a term and returns True iff the coefficient \ ## is positive def is_positive(self): return self.coeff > 0 class polynomial: """ Field: term term is the head of a linked list representing a polynomial.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern