Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- def findNTheCoefficients(binomial):
- coefficientsList = []
- # **************************************************************************************************************
- # **************************************************************************************************************
- # 1. FOR FINDING a in expression : "ax + b"
- # I will create a variable named "index1". This variable will store the index/position of letter 'x' in binomial
- indexOfX = -1000
- str1 = ""
- coefficientA = 0
- for i in range(0, len(binomial)):
- if binomial[i] == 'x':
- indexOfX = i
- break
- # I have found the position of 'x' in the binomial
- # Now, I want the string (numbers exactly as string) BEFORE the position of 'x'
- str1 = binomial[0 : indexOfX]
- # Now, str1 contains what's before the letter 'x'
- # There is a possibility the coefficient of 'x' in binomial is 1, so that means that "str1" is empty
- if str1 == "":
- coefficientA = 1
- elif str1 == "-" or str1 == "- ":
- coefficientA = -1
- else:
- # In the case: coefficientA != 1 and coefficientA != -1, we know that coefficientA is sth like: 28 or -14
- if binomial[0] == '-':
- str1 = binomial[1:indexOfX] # I put 1 in first parameter to throw out the minus sign
- coefficientA = - int(str1)
- else: # Binomial starts with positive number
- str1 = binomial[0:indexOfX]
- coefficientA = int(str1)
- # **************************************************************************************************************
- # **************************************************************************************************************
- # 2. Finding the standard coeffficient b in expression: "ax + b"
- indexOfPlusMinusSign = -1000
- str2 = ""
- coefficientB = 0
- isItPlusSign = True # For the second term
- # I will find the position of '+' or '-': When I find it, I know that
- # the position of my number begins 2 positions later (because of the SPACEBAR between them)
- for i in range(0, len(binomial)):
- if binomial[i] == '+':
- indexOfPlusMinusSign = i
- isItPlusSign = True
- break
- elif binomial[i] == '-':
- indexOfPlusMinusSign = i
- isItPlusSign = False
- break
- # I will create the str2
- str2 = binomial[(indexOfPlusMinusSign+2) : len(binomial)]
- if isItPlusSign == True:
- coefficientB = int(str2)
- else:
- coefficientB = -int(str2)
- coefficientsList.append(coefficientA)
- coefficientsList.append(coefficientB)
- return coefficientsList
- def factorial(n):
- if n == 0:
- return 1
- else:
- return n * factorial(n-1)
- def c(n,k):
- return int( factorial(n) / (factorial(n-k) * factorial(k)) )
- # ************************************************************************************************************
- # ************************************************************************************************************
- # (ax+b)^n = Σ (k=0 to n) ( c(n,k) * a^k * b^(n-k) )
- def multiplyByMyself(coefficientsList, n):
- a = coefficientsList[0]
- b = coefficientsList[1]
- multiplyResultList = []
- for i in range(0, n+1): # From 0 to n
- multiplyResultList.append( (a**i) * (b**(n-i)) * c(n,i) )
- # Reverse the list to have the a-big powers first
- multiplyResultList.reverse()
- return multiplyResultList
- def convertPolyonymoIntoString(polyonymo):
- # Binomial is a list: [4, -12, 9] ---> I have to make it a string
- n = len(polyonymo) - 1
- exponents = []
- for i in range(0, n+1):
- exponents.append(n-i)
- # Now, list "exponents" contains all the exponents, matching in the terms of the binomial
- # EXAMPLE
- # polyonymo = [4, -12, 9] .... standing for input: binomial = "2x - 3" and exponent 2
- # exponents = [2, 1, 0]
- # I have to match them together
- stringResult = ""
- for i in range(0, len(polyonymo)):
- #print(polyonymo[i])
- #print(exponents[i])
- # FOR EXPONENT = 0 ---> STANDARD COEFFICIENT
- if exponents[i] == 0 and polyonymo[i] > 0:
- stringResult += (' + ' + str(polyonymo[i]))
- elif exponents[i] == 0 and polyonymo[i] < 0:
- stringResult += (' - ' + str(abs(polyonymo[i])))
- # FOR EXPONENT = 1 ---> TERMS IN FORM "ax"
- elif exponents[i] == 1 and polyonymo[i] > 0 and polyonymo[i] != 1:
- stringResult += (' + ' + str(polyonymo[i]) + 'x')
- elif exponents[i] == 1 and polyonymo[i] == 1:
- stringResult += (' + x')
- elif exponents[i] == 1 and polyonymo[i] < 0 and polyonymo[i] != -1:
- stringResult += (' - ' + str(abs(polyonymo[i])) + 'x')
- elif exponents[i] == 1 and polyonymo[i] == -1:
- stringResult += (' - x')
- # FOR EXPONENT = 2, 3, 4, 5, ....
- elif exponents[i] > 1 and polyonymo[i] > 0 and polyonymo[i] != 1:
- stringResult += (' + ' + str(polyonymo[i]) + 'x^' + str(exponents[i]))
- elif exponents[i] > 1 and polyonymo[i] == 1:
- stringResult += (' + x^' + str(exponents[i]))
- elif exponents[i] > 1 and polyonymo[i] < 0 and polyonymo[i] != -1:
- stringResult += (' - ' + str(abs(polyonymo[i])) + 'x^' + str(exponents[i]))
- elif exponents[i] > 1 and polyonymo[i] == -1:
- stringResult += (' - x^' + str(exponents[i]))
- # NOW, OUR STRING IS ALMOST COMPLETED WITH 2 PROBLEMS
- # Problem 1: All strings begin with one spacebar, so I have to remove it
- stringResult = stringResult[1:len(stringResult)]
- # Problem 2: If the new string has positive coefficient in front of the biggest term (in rank), I will remove
- # the '+' sign and the following spacebar
- if stringResult[0] == '+': # That means that the 1st term is with positive coefficient
- stringResult = stringResult[2:len(stringResult)]
- return stringResult
- # *****************************************************************************************************************
- # *****************************************************************************************************************
- # *****************************************************************************************************************
- # *****************************************************************************************************************
- # Final functions that contains the above functions
- def ex_binomial(binomial, power):
- coefficientsOfBinomial = findNTheCoefficients(binomial) # LIST of binomial coefficients
- polyonymo = multiplyByMyself(coefficientsOfBinomial, power) # LIST of polyonymo coefficients
- answer = convertPolyonymoIntoString(polyonymo)
- return answer
- # MAIN FUNCTION
- print("(2x + 3)^2 = " + ex_binomial("2x + 3", 2))
- print("(x - 1)^3 = " + ex_binomial("x - 1", 3))
Add Comment
Please, Sign In to add comment