1

I'm in high school, (don't judge) and I've recently been working on coding a synthetic division calculator, and as of right now, it works. However, as you can see from the below record of the output and input, the information gathering stage is quite tedious.

Enter the degree of the numerator (max 12):
 3
Enter the coefficient of x^3 in the numerator:
 2
Enter the coefficient of x^2 in the numerator:
 -6
Enter the coefficient of x in the numerator:
 -3
Enter the value of the constant in the numerator:
 8
Enter the degree of the denominator (max 2):
 2
Enter the coefficient of x^2 in the denominator:
 7
Enter the coefficient of x in the denominator:
 -5
Enter the value of the constant in the denominator:
 3
The quotient of the numerator 2x^3-6x^2-3x+8 and the denominator 7x^2-5x+3
 is equal to (2/7)x+(-32/49)+((-349/49)x+(488/49))/(7x^2-5x+3) which is
 (0.285714285714)x+(-0.65306122449)+((-7.12244897959)x+(9.95918367347))/(7x^2-5x+3)
 in rounded decimal format.

What I want is to turn this:

import re
numerator = '7x^12-6x^5+2/3x-19'
for m in re.finditer( r'(-{0,1}\d*)x\^{0,1}(-{0,1}\d*)', numerator):
  coef, expn = list(map( lambda x: x if x != '' and x != '-' else x + '1' , m.groups( ) ))
  print ('coef:{}, exp:{}'.format( coef, expn ))

Into something like this:

import re
numerator = '7x^12-6x^5+2/3x-19'
for m in re.finditer( r'(-{0,1}\d*)x\^{0,1}(-{0,1}\d*)', numerator):
  coef, expn = list(map( lambda x: x if x != '' and x != '-' else x + '1' , m.groups( ) ))
  [A, B, C, ... , M] = #??? 

#so "print [A, B, C, ... , M]" would print "7, 0, 0, 0, ... ,-6 , ... , -19"
#currently, it completly misses the point and prints "coef:7, exp:12, coef:-6, exp:5, coef:3, exp:-19"

The goal is to be able to simply input 2x^3-6x^2-3x+8 and 7x^2-5x+3, or any other combination of polynomials, and have the program convert each coefficient into a variable. If this is to complicated (I'm sure it can be done, but I don't have the slightest idea how) then it might be easier to have the program look at what is in between parentheses. This would require the user to be more careful when inputting polynomials because a single extra "(" could potentially mess up the whole thing, but it is significantly easier, I would be satisfied.

Lewis
  • 23
  • 4
  • Please show us some of your code. You seem to have a question about parsing equations, perhaps you can clarify it. – Michael Shopsin Jan 24 '18 at 15:34
  • @MichaelShopsin: I believe that's the point of the question: there *is* no parsing code as of yet, merely a soporific series of `input` lines. – Prune Jan 24 '18 at 15:45
  • 1
    Being in high school is hardly a felony -- although I have a co-worker who refers to K-12 schools as "institutions for the criminally youthful". Welcome to Stack Overflow. This is a good place to work on building your technical vocabulary, so you can search for solutions. I found the duplicate by searching on "Python parse polynomial". You may want to learn regular expressions, recursive-descent parsing, and other ways to parse input with non-trivial semantics. – Prune Jan 24 '18 at 15:52
  • Thanks-- I had no idea "parsing" was what I was looking for, otherwise I probably would have found something. – Lewis Jan 24 '18 at 16:00
  • @Lewis I started programming seriously in high school before the web and had to rely in bookstores and friends to answer my programming questions. Keep at the parsing problem and edit the question as you need more. – Michael Shopsin Jan 24 '18 at 16:15
  • As an alternative, you could ask the user to input the coefficients separated by spaces on a single line. Parsing this kind of input is much simpler than parsing all the x's and exponents, let alone more general parsing for any mathematical expression. Note that you can deduce the degree of the polynomial from the number of coefficients. – Code-Apprentice Jan 24 '18 at 16:34
  • Good point... If nobody can give me an answer, I'll have to do that. @Code-Apprentice – Lewis Jan 24 '18 at 16:37
  • @MichaelShopsin Thanks, I'll keep that in mind. – Lewis Jan 24 '18 at 16:38
  • Note that your question has been put on hold. This means that no one is allowed to answer it. The comments and link at the top of this page should provide information to get you started if you decide you want to write a parser for the input format you originally asked about. – Code-Apprentice Jan 24 '18 at 16:42
  • print poly poly=list(poly.split(',')) for i in range(0,13): if int(poly[i*2+1])!=int(12-i): poly=poly.insert((i*2+1),str(12-i)) poly=poly.insert((i*2+1),"0") – Lewis Jan 27 '18 at 12:33
  • what am I doing wrong? – Lewis Jan 27 '18 at 12:33

0 Answers0