-1

I need to find a simple method for storing a specific whole number in say- a polynomial. If the user inputs:

2x^3 + 5x^2 - 8x + 3

I basically want to create a list (thinking this will be the easiest method) of [2, 5, -8, 3] as f(x) and then another list for g(x) so I can add them/subtract them later. I'm completely stumped on how to do this and I want the user to input the whole polynomial at once. I dont want my program to ask it in parts. Thanks:) (PS I'm going out for about half an hour/45 min so I will get back to this when I am home. Thanks again!)

Jacob Smith
  • 123
  • 8
  • 1
    Have you tried using the `input` function? Doing that and then using regular expressions to parse it is probably the easiest way. – skrrgwasme Dec 11 '18 at 01:27
  • Possible duplicate of [Equation parsing in Python](https://stackoverflow.com/questions/594266/equation-parsing-in-python) – tripleee Dec 12 '18 at 06:18

2 Answers2

2

You could use sympy which "understands" polynomials. You'd still have to insert multiplication signs manually, though:

import re, sympy

# example
s = '2x^3 + 5x^2 - 8x + 3'
# replace juxtapostion with explicit multiplication
sp = re.sub('[0-9][a-z]', lambda m: '*'.join(m.group()), s)
sp
# '2*x^3 + 5*x^2 - 8*x + 3'
# no we can create a poly object
p = sympy.poly(sp)
p
Poly(2*x**3 + 5*x**2 - 8*x + 3, x, domain='ZZ')
# getting coefficients is easy
p.coeffs()
[2, 5, -8, 3]
# and we can do all sorts of other poly stuff 
p*p
Poly(4*x**6 + 20*x**5 - 7*x**4 - 68*x**3 + 94*x**2 - 48*x + 9, x, domain='ZZ')
...
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0

Use re (regex) to do this pattern finding stuff, and use input to get the text entered:

import re
a=input('Enter your stuff: ')
s=re.sub('[a-zA-Z^]','',a)
print([int('-'+i[0]) if s[s.index(i)-2]=='-' else int(i[0]) for i in re.split(' [+|-] ',s)])

Example Output:

Enter your stuff: 2x^3 + 5x^2 - 8x + 3
[2, 5, -8, 3]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    This is incredible! I've been doing some research on regex because I've never heard of it before... I understand that at the s=re.sub line you are scanning every character that is from a-z or A-Z but you have the carrot at the end and not the beginning so how does that change the "not" statement? Also what is the ' ' statement doing before you pass in the var "a". And if you could help me understand the print statement at the very end that would be amazing too. Thank you so much :) – Jacob Smith Dec 11 '18 at 03:58
  • 1
    Edit: I've figured out what everything in the sub line is and how the carrot is actually just what we're taking out. I will get to work on the print. – Jacob Smith Dec 11 '18 at 04:31
  • @JacobSmith Got back to my computer, can help you to explain: Use `re.split` to split on `' - '` and `' + '` then get the first letter of each element, then use a condition too see if before it was `' - '` or not, if it was, make it negative, otherwise, don't make changes. – U13-Forward Dec 11 '18 at 07:41
  • 1
    I don't see why you bothered with the `s=re.sub('[a-zA-Z^]','',a)` part - it doesn't seem necessary. Also, this doesn't handle two-digit coefficients, implicit "1" coefficients, decimal points, or a minus sign on the leading term. (Also, `|` isn't alternation inside a character class. The `|` should just be removed.) – user2357112 Dec 12 '18 at 05:57
  • 1
    Also, the index returned by `index` is frequently not the index you need, leading to results like [`'3x^2 - 3x - 3'` getting parsed as `[3, 3, 3]`, with no negative elements](https://ideone.com/0RPDZ9). – user2357112 Dec 12 '18 at 06:01
  • See also now this related question: https://stackoverflow.com/questions/53736633/unpack-regex-list-comprehension – tripleee Dec 12 '18 at 06:08