a2solutions

a2solutions - import re, copy # index_from_right: string...

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

View Full Document Right Arrow Icon
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:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

a2solutions - import re, copy # index_from_right: string...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online