1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| def solution(polynomial):
coef = 0; const = 0
for ch in polynomial.split("+"):
_ch = ch.strip()
if 'x' in _ch:
_ch = _ch.replace("x","")
if _ch.isdigit():
coef += int(_ch)
else:
coef += 1
elif _ch.isdigit():
const += int(_ch)
if coef == 0 and const != 0:
return str(const)
elif coef != 0 and const == 0:
return str(coef) + "x"
elif coef != 0 and const != 0:
return str(coef) + "x" + " + " + str(const)
elif coef == 0 and const == 0:
return "0"
|