-
Notifications
You must be signed in to change notification settings - Fork 0
/
0640.py
37 lines (36 loc) · 1.26 KB
/
0640.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def solveEquation(self, equation: str) -> str:
left, right = equation.split("=")[0], equation.split("=")[1]
def helper(s):
flag = 1 #1 means +; -1 means -
num_x = 0
num_n = 0
cur_num = ""
for c in s:
if c == "x":
if not cur_num:
num_x += flag*1
else:
num_x += flag*(int(cur_num))
cur_num = ""
elif c == "+":
if cur_num:
num_n += flag*(int(cur_num))
cur_num = ""
flag = 1
elif c == "-":
if cur_num:
num_n += flag*(int(cur_num))
cur_num = ""
flag = -1
else:
cur_num += str(c)
if s[-1].isdigit():
num_n += flag*(int(cur_num))
return num_x, num_n
x1, n1 = helper(left)
x2, n2 = helper(right)
r_x, r_n = x1 - x2, n2 - n1
if r_x == 0 and r_n == 0: return "Infinite solutions"
if r_x == 0: return "No solution"
return "x="+ str(r_n//r_x)