makispaiktis

Multiply Binomials

Oct 27th, 2019 (edited)
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.01 KB | None | 0 0
  1. import math
  2.  
  3. def findNTheCoefficients(binomial):
  4.  
  5.     coefficientsList = []
  6.     # **************************************************************************************************************
  7.     # **************************************************************************************************************
  8.     # 1. FOR FINDING a in expression : "ax + b"
  9.     # I will create a variable named "index1". This variable will store the index/position of letter 'x' in binomial
  10.     indexOfX = -1000
  11.     str1 = ""
  12.     coefficientA = 0
  13.     for i in range(0, len(binomial)):
  14.         if binomial[i] == 'x':
  15.             indexOfX = i
  16.             break
  17.  
  18.     # I have found the position of 'x' in the binomial
  19.     # Now, I want the string (numbers exactly as string) BEFORE the position of 'x'
  20.     str1 = binomial[0 : indexOfX]
  21.     # Now, str1 contains what's before the letter 'x'
  22.     # There is a possibility the coefficient of 'x' in binomial is 1, so that means that "str1" is empty
  23.     if str1 == "":
  24.         coefficientA = 1
  25.     elif str1 == "-" or str1 == "- ":
  26.         coefficientA = -1
  27.     else:
  28.         # In the case: coefficientA != 1 and coefficientA != -1, we know that coefficientA is sth like: 28 or -14
  29.         if binomial[0] == '-':
  30.             str1 = binomial[1:indexOfX]         # I put 1 in first parameter to throw out the minus sign
  31.             coefficientA = - int(str1)
  32.         else:                               # Binomial starts with positive number
  33.             str1 = binomial[0:indexOfX]
  34.             coefficientA = int(str1)
  35.  
  36.     # **************************************************************************************************************
  37.     # **************************************************************************************************************
  38.     # 2. Finding the standard coeffficient b in expression: "ax + b"
  39.     indexOfPlusMinusSign = -1000
  40.     str2 = ""
  41.     coefficientB = 0
  42.     isItPlusSign = True             # For the second term
  43.     # I will find the position of '+' or '-': When I find it, I know that
  44.     # the position of my number begins 2 positions later (because of the SPACEBAR between them)
  45.     for i in range(0, len(binomial)):
  46.         if binomial[i] == '+':
  47.             indexOfPlusMinusSign = i
  48.             isItPlusSign = True
  49.             break
  50.         elif binomial[i] == '-':
  51.             indexOfPlusMinusSign = i
  52.             isItPlusSign = False
  53.             break
  54.     # I will create the str2
  55.     str2 = binomial[(indexOfPlusMinusSign+2) : len(binomial)]
  56.     if isItPlusSign == True:
  57.         coefficientB = int(str2)
  58.     else:
  59.         coefficientB = -int(str2)
  60.  
  61.     coefficientsList.append(coefficientA)
  62.     coefficientsList.append(coefficientB)
  63.     return coefficientsList
  64.  
  65.  
  66.  
  67.  
  68. def factorial(n):
  69.     if n == 0:
  70.         return 1
  71.     else:
  72.         return n * factorial(n-1)
  73.  
  74.  
  75. def c(n,k):
  76.     return int( factorial(n) / (factorial(n-k) * factorial(k)) )
  77.  
  78.  
  79. # ************************************************************************************************************
  80. # ************************************************************************************************************
  81. # (ax+b)^n = Σ (k=0 to n) ( c(n,k) * a^k * b^(n-k)  )
  82. def multiplyByMyself(coefficientsList, n):
  83.     a = coefficientsList[0]
  84.     b = coefficientsList[1]
  85.     multiplyResultList = []
  86.     for i in range(0, n+1):                             # From 0 to n
  87.         multiplyResultList.append( (a**i) * (b**(n-i)) * c(n,i) )
  88.  
  89.     # Reverse the list to have the a-big powers first
  90.     multiplyResultList.reverse()
  91.     return multiplyResultList
  92.  
  93.  
  94. def convertPolyonymoIntoString(polyonymo):
  95.     # Binomial is a list: [4, -12, 9] ---> I have to make it a string
  96.     n = len(polyonymo) - 1
  97.     exponents = []
  98.     for i in range(0, n+1):
  99.         exponents.append(n-i)
  100.     # Now, list "exponents" contains all the exponents, matching in the terms of the binomial
  101.     # EXAMPLE
  102.     # polyonymo = [4, -12, 9]  ....  standing for input: binomial = "2x - 3" and exponent 2
  103.     # exponents = [2, 1, 0]
  104.     # I have to match them together
  105.     stringResult = ""
  106.     for i in range(0, len(polyonymo)):
  107.         #print(polyonymo[i])
  108.         #print(exponents[i])
  109.         # FOR EXPONENT = 0 ---> STANDARD COEFFICIENT
  110.         if exponents[i] == 0 and polyonymo[i] > 0:
  111.             stringResult += (' + ' + str(polyonymo[i]))
  112.         elif exponents[i] == 0 and polyonymo[i] < 0:
  113.             stringResult += (' - ' + str(abs(polyonymo[i])))
  114.         # FOR EXPONENT = 1 ---> TERMS IN FORM "ax"
  115.         elif exponents[i] == 1 and polyonymo[i] > 0 and polyonymo[i] != 1:
  116.             stringResult += (' + ' + str(polyonymo[i]) + 'x')
  117.         elif exponents[i] == 1 and polyonymo[i] == 1:
  118.             stringResult += (' + x')
  119.         elif exponents[i] == 1 and polyonymo[i] < 0 and polyonymo[i] != -1:
  120.             stringResult += (' - ' + str(abs(polyonymo[i])) + 'x')
  121.         elif exponents[i] == 1 and polyonymo[i] == -1:
  122.             stringResult += (' - x')
  123.         # FOR EXPONENT = 2, 3, 4, 5, ....
  124.         elif exponents[i] > 1 and polyonymo[i] > 0 and polyonymo[i] != 1:
  125.             stringResult += (' + ' + str(polyonymo[i]) + 'x^' + str(exponents[i]))
  126.         elif exponents[i] > 1 and polyonymo[i] == 1:
  127.             stringResult += (' + x^' + str(exponents[i]))
  128.         elif exponents[i] > 1 and polyonymo[i] < 0 and polyonymo[i] != -1:
  129.             stringResult += (' - ' + str(abs(polyonymo[i])) + 'x^' + str(exponents[i]))
  130.         elif exponents[i] > 1 and polyonymo[i] == -1:
  131.             stringResult += (' - x^' + str(exponents[i]))
  132.  
  133.     # NOW, OUR STRING IS ALMOST COMPLETED WITH 2 PROBLEMS
  134.     # Problem 1: All strings begin with one spacebar, so I have to remove it
  135.     stringResult = stringResult[1:len(stringResult)]
  136.     # Problem 2: If the new string has positive coefficient in front of the biggest term (in rank), I will remove
  137.     # the '+' sign and the following spacebar
  138.     if stringResult[0] == '+':                          # That means that the 1st term is with positive coefficient
  139.         stringResult = stringResult[2:len(stringResult)]
  140.  
  141.     return stringResult
  142.  
  143.  
  144. # *****************************************************************************************************************
  145. # *****************************************************************************************************************
  146. # *****************************************************************************************************************
  147. # *****************************************************************************************************************
  148. # Final functions that contains the above functions
  149. def ex_binomial(binomial, power):
  150.     coefficientsOfBinomial = findNTheCoefficients(binomial)         # LIST of binomial coefficients
  151.     polyonymo = multiplyByMyself(coefficientsOfBinomial, power)     # LIST of polyonymo coefficients
  152.     answer = convertPolyonymoIntoString(polyonymo)
  153.     return answer
  154.  
  155.  
  156. # MAIN FUNCTION
  157. print("(2x + 3)^2 = " + ex_binomial("2x + 3", 2))
  158. print("(x - 1)^3 = " + ex_binomial("x - 1", 3))
Add Comment
Please, Sign In to add comment