Reputation: 123
Did some research, and asked someone on Stack if they could help me with a polynomial converter to take a polynomial and turn it into a list - Ex: 3x^2 - 8x + 5
--- [3, -8, 5]
. They did an excellent job with it, however it is a bit over my head in terms of whats going on. Was wondering if anyone would be willing to give me a detailed breakdown on what's happening and help me rewrite it to make sure I understand what's going on when I come back to it. (Using regex btw) Here it is:
poly = ("4x^2 - 3x + 2")
s=re.sub('[xX^]','',poly)
print([int('-'+i[0]) if s[s.index(i)-2]=='-' else int(i[0]) for i in re.split(' [+|-] ',s)])
I tried putting it in terms that make sense to me order-wise but I don't really know what to do because it doesn't work.
if s[s.index(i) - 2]=="-":
int("-" + i[0])
else:
int(i[0])
for i in re.split:
(" [+|-] ", s)
Where am I going wrong with this?
Upvotes: 0
Views: 206
Reputation: 1220
Interesting problem!
Another classic case of Python over-compression! List comprehensions can be a nightmare, and I hate to see them thrown at beginners. I also noticed a few potential problems with that approach, so I'd like to offer an alternative. For a value add, this will also keep track of the power of each term. I've commented the approach.
# This is hopefully self-explanatory.
poly = "4x^2 - 3x^3 - 2x + 2"
# We made this regex more complicated in order to capture an entire
# term of a polynomial. Here's how that works:
# ([+-]|) - The first capture group, contain the sign or (|)
# nothing.
# \s* - Allow whitespace between the sign and the term.
# ([0-9]+) - The second capture group, contain the coefficient.
# ([xX]|) - Match the variable or nothing.
# (\^[0-9]+|) - Match the exponent or nothing. Notice we escape the caret.
matches = re.finditer(r'([+-]|)\s*([0-9]+)([xX]|)(\^[0-9]+|)', poly)
# Now we parse the results of the regex...
terms = list() # Create a list to store our processed terms.
for match in matches: # Iterate our matches.
# Step 1: Parse the groups' contents.
negative = match.group(1) == '-' # Check if the sign is negative.
coef = int(match.group(2)) # Retrieve the coefficient as an integer.
has_variable = bool(match.group(3)) # Check whether there's a variable.
exponent_str = match.group(4) # Get the exponent group.
if exponent_str:
# There's an exponent, remove the caret and turn it into an
# integer.
exponent = int(exponent_str[1:])
else:
# There's no exponent, if there's a variable the power of this term
# is 1, otherwise it's 0.
exponent = 1 if has_variable else 0
# Step 2: Create a (coefficient, power) tuple and add it to the list of
# terms.
if negative:
# Make the coefficient negative if the sign was a -.
coef *= -1
# Add a tuple containing the coefficient and exponent to our list of
# terms.
terms.append((coef, exponent))
# Optional: sort the terms in order of power (descending).
terms.sort(key=lambda tpl: -tpl[1])
# Print the output.
print(terms)
This will output
[(-3, 3), (4, 2), (-2, 1), (2, 0)]
Upvotes: 1
Reputation: 506
Try this:
for i in re.split(' [+|-] ',s):
if s[s.index(i) - 2]=="-":
x.append(int("-" + i[0]))
else:
x.append(int(i[0]))
print(x)
Output:
[4, -3, 2]
First, You need to understand how list comprehension works, in your code
[ `firstValue` if `condition` else `secondValue` while iterating over list]
So, the this small code, is iterating over a for loop (the for loop should be outside) evaluating condition on each element, and appending the output to main list.
Upvotes: 2