splitting terms in a polynomial expression without external libraries like sympy [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Say i inputted "1+2x+3x^2" or "1-2x+3x^2"
How do i make a function that splits and makes a list of each term like [1, 2x, 3x^2] or [1, -2x, 3x^2].
I have been stumped at this for a while, for now the function im using currently, seperates only at the "+" so to get a list like [1, -2x, 3x^2] i have to input
"1+-2x+3x^2"
Note: I won't be using complex polynomials but simple looking ones that doesn't include parenthesis or fractions.

import re
s = '1+2x^2-3x^-3'
s = re.sub('\^-', 'neg', s) # trick to deal the negative powers
print(s)
s=s.replace('-','+-')
s=s.replace('neg','-')
sub = s.split('+')
print(sub)
This will split your negative power variables correctly.

Related

How can I sort the a list of strings which contain date as substring [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Below is my list,
['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
And I need to sort the list and filter it to a below format. Please suggest a quicker way to achieve it
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
When your format is fixed and always starts with "pending" you can use the normal sorted function and count the / in a list comprehension.
>>> values = ['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
>>> sorted(x for x in values if x.count('/') == 2)
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']

Sort list that includes float numbers and words with number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to sort a list with the following values
My list
list_to_sort = ['1.1.0','1.2.3','1.3.10','1.3.9','x23','hi']
My expected output
['1.1.0','1.2.3','1.3.9','1.3.10','hi','x23']
What i have tried:
list_to_sort .sort()
I have never done something like this so I would love to know how I could solve it. Thanks!
This was, needless to say, an interesting one to solve
list_to_sort = ['1.1.0', '1.2.0','4.3.0' ,'1.3.10','1.3.9','1.31.0','x23','hi']
def weirdFloats(x):
try:
#trips an error when given something like "hi"
return [int(i) for i in x.split(".")]
except:
return [float("Inf"), x]
print(sorted(list_to_sort, key= weirdFloats, reverse= False))
['1.1.0', '1.2.0', '1.3.9', '1.3.10', '1.31.0', '4.3.0', 'hi', 'x23']
Note that im not sure if this properly accounts for things that cant be interpreted as what you call "floats" (they look more like version #'s to me) such as what seem to be the outliers here hi and x23

how to find element that has no duplicate number? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need your help here.
by use Python, I want to find a way to filter out element in list that has no double integer(at least 2 continuous number).
like in [314120,420423,432192,444689,112345], I want filter out 314120 and 420423,432192.
because 444689 has 444 and 112345 has 11. so they shall not be filter out as a expected result.
thank you
You could simply use something like this:
import re
result = [x for x in numbers if !re.search(r"(.)\1", str(x))]
This document represents a way to check the uniqueness of characters. Suppose isUniqueChars is the method and numbers is your list of integers, you can find the one which all its numbers are unique by following piece of code:
unique = [n for n in numbers if isUniqueChars(str(n))]

Splitting a line of complex string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to split a really complex line for me. The line I want to split is as follows
2019.10.20-22.01.33: '10.11.111.25 9999995555884411:TechnoBeceT(69)' logged in
how can i split this like this
['2019.10.20-22.01.33', '10.11.111.25', '9999995555884411', 'logged in']
i don't need
TechnoBeceT(69) this area.
Using Regular Expression
import re
p = re.compile(r'(([\d\.-]+)(?::|\s)|(logged in))')
s = "2019.10.20-22.01.33: '10.11.111.25 9999995555884411:TechnoBeceT(69)' logged in"
q = [x[1] or x[2] for x in p.findall(s)]
print(q)
Output
['2019.10.20-22.01.33', '10.11.111.25', '9999995555884411', 'logged in']
Looks like you just need to split by ' ', ':' and 'TechnoBeceT(69)' as an appropriate regex. This existing question is probably what you need: Split string with multiple delimiters in Python

get everything in a string after a specific symbol in python using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Hi I would like to get everything after the '_' using regex
For example: I have --> I want
'aaa_bbb_ccc' --> 'bbb_ccc'
'dd_aaaa_1' --> 'aaaa_1'
'*/_2d*_//' --> '2d*_//'
Is there anyway to do it?
Thanks in advance.
I rather like the split suggestion given by #Maroun in a comment above. Here is an option using re.sub:
x = "aaa_bbb_ccc"
output = re.sub(r'^[^_]+_', '', x)
print(output)
bbb_ccc
The regex does not require much explanation, and it just removes all content up to, and including, the first underscore in the input string.

Categories