Is there any way to search for a sequence by order (which is not continuous in the input) in a string in one command or a short block?
I'll give an example to the problem:
I gen a string from raw_input
in the form of (+ or - or void)(number or void)X^2 (+ or -) (+ or - or void)X (+ or -) (+ or - or void)(number)
, and I want to assign a flag for (-+)aX^2 +- (-+)bX +- (+-)c
and a flag for only c
and a flag for only b
... only b
and c
, only a
and c
, etc in any order of input (abc, cba, ac, cb, etc)
by defining a search by characters "x^2", "x"
"number" and assigning each order to a flag...
Can it be done without writing 300+ lines with tons of if
s and elif
s? (I got frustrated in line 107).
I have tried the long agonizing way:
def sorting_abc():
polinput = raw_input("insert a pol.: ") # input as string
a = 0
b = 0
c = 0
temp = [] * 5
splinput = polinput.split() #splitted string input
lensplin = len(splinput) #Length of splinput
temp = splinput
flag_1var = bool(lensplin == 1)
flag_2var = bool(lensplin == 3)
flag_3var = bool(lensplin == 5)
flag_minusplus = bool(temp[1] == '-' and temp[3] == '+')
flag_plusminus = bool(temp[1] == '+' and temp[3] == '-')
flag_minusminus = bool(temp[1] == '-' and temp[3] == '-')
flag_plusplus = bool(temp[1] == '+' and temp[3] == '+')
flag_2var_minus = bool(splinput <= 3 and temp[1] == '-')
flag_2var_plus = bool(splinput <= 3 and temp[1] == '+')
if (flag_1var):
p1 = tamp[0]
p1i = p1.find('x^2')
a = p1[:p1i:]
if (a == ''):
a = 1
if (a == '-'):
a = -1
a = int(a)
if (flag_2var):
p1 = temp[0]
p2 = temp[1]
p3 = temp[2]
if ('x^2' in p1):
p1i = p1.find('x^2')
a = p1[:p1i:]
if (a == ''):
a = 1
if (a == '-'):
a = -1
c = p3
if (p3 == '-'):
c -= int(c)
if ('x^2' in p3):
p3i = p3.find('x^2')
a = p3[:p3i:]
if (a == ''):
a = 1
if (p2 == '-'):
a -= int(a)
c = p1
if (flag_3var):
p1 = temp[0]
p2 = temp[1]
p3 = temp[2]
p4 = temp[3]
p5 = temp[4]
if ('x^2' in p1):
p1i = p1.find('x^2')
a = p1[:p1i:]
if (a == ''):
a = 1
if (a == '-'):
a = -1
if ('x' in p3):
p3i = p3.find('x')
b = p3[:p3i:]
if (b == ''):
b = 1
if (p2 == '-'):
b -= int(b)
c = p5
if (p4 == '-'):
c -= int(c)
if ('x' in p5):
p5i = p5.find('x')
b = p5[:p5i:]
if (b == ''):
b = 1
if (p4 == '-'):
b -= int(b)
if (p2 == '-'):
c -= int(c)
c = p3
elif ('x^2' in p3):
p3i = p3.find('x^2')
a = p3[:p3i:]
if (a == ''):
Any chance to shorten this significantly?