Home > Net >  How to remove 1 as a coefficient on my Python Code?
How to remove 1 as a coefficient on my Python Code?

Time:12-06

I have been looking at my code for awhile and need to return a polynomial given a dictionary. The polynomial output should not have any 1's as coefficients. Her e is my code:

class Polynomial:
  # Constructor
  def __init__(self, polyDict):
    self.polyDict = polyDict

  # String Method   
  def __str__(self):
    polyStr = ""
    firstTerm = True
    for exp, coeff in sorted(self.polyDict.items(), reverse=True):
      if coeff == 0:
        continue
      if firstTerm:
        if coeff > 0:
          polyStr  = str(coeff)
        else:
          polyStr  = str(coeff)
        if exp > 1:
          polyStr  = "x^"   str(exp)
        elif exp == 1:
          polyStr  = "x"
        firstTerm = False
      else:
        if coeff > 0:
          polyStr  = " "   str(coeff)
        else:
          polyStr  = str(coeff)
        if exp > 1:
          polyStr  = "x^"   str(exp)
        elif exp == 1:
          polyStr  = "x"
    return polyStr

print(Polynomial({6:-3, 5:6, 4:-1, 3:-2, 2:0, 1:1, 0:-3}))

I am unsure what else to try. I've staired at the code for a couple hours and dont know what to add.

CodePudding user response:

To fix the issue with the polynomial output having 1's as coefficients, you can modify the str method to check if the coefficient is 1 or -1 and not print it in the string representation of the polynomial. Here is an updated implementation of the str method that does this:

def __str__(self):
    polyStr = ""
    firstTerm = True
    for exp, coeff in sorted(self.polyDict.items(), reverse=True):
        if coeff == 0:
            continue
        if firstTerm:
            if coeff > 0:
                if coeff != 1:
                    polyStr  = str(coeff)
                if exp > 1:
                    polyStr  = "x^"   str(exp)
                elif exp == 1:
                    polyStr  = "x"
            else:
                if coeff != -1:
                    polyStr  = str(coeff)
                else:
                    polyStr  = "-"
                if exp > 1:
                    polyStr  = "x^"   str(exp)
                elif exp == 1:
                    polyStr  = "x"
            firstTerm = False
        else:
            if coeff > 0:
                if coeff != 1:
                    polyStr  = " "   str(coeff)
                else:
                    polyStr  = " "
                if exp > 1:
                    polyStr  = "x^"   str(exp)
                elif exp == 1:
                    polyStr  = "x"
            else:
                if coeff != -1:
                    polyStr  = str(coeff)
                else:
                    polyStr  = "-"

CodePudding user response:

I think this combines as much as can be combined.

class Polynomial:
    # Constructor
    def __init__(self, polyDict):
        self.polyDict = polyDict

    # String Method     
    def __str__(self):
        polyStr = ""
        firstTerm = True
        for exp, coeff in sorted(self.polyDict.items(), reverse=True):
            if coeff == 0:
                continue
            if coeff < -1 or (firstTerm and coeff > 1):
                polyStr  = str(coeff)
            elif coeff == -1:
                polyStr  = '-'
            elif coeff == 1:
                if not firstTerm:
                    polyStr  = ' '
            else:
                polyStr  = ' ' str(coeff)
            firstTerm = False

            if exp > 1:
                polyStr  = "x^" str(exp)
            elif exp == 1:
                polyStr  = "x"
        return polyStr

print(Polynomial({6:-3, 5:6, 4:-1, 3:-2, 2:0, 1:1, 0:-3}))
  • Related