-
Notifications
You must be signed in to change notification settings - Fork 43
/
solve-the-equation.py
160 lines (137 loc) · 4.98 KB
/
solve-the-equation.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
640. Solve the Equation
Medium
Solve a given equation and return the value of 'x' in the form of a string "x=#value". The equation contains only '+', '-' operation, the variable 'x' and its coefficient. You should return "No solution" if there is no solution for the equation, or "Infinite solutions" if there are infinite solutions for the equation.
If there is exactly one solution for the equation, we ensure that the value of 'x' is an integer.
Example 1:
Input: equation = "x+5-3+x=6+x-2"
Output: "x=2"
Example 2:
Input: equation = "x=x"
Output: "Infinite solutions"
Example 3:
Input: equation = "2x=x"
Output: "x=0"
Example 4:
Input: equation = "2x+3x-6x=x+2"
Output: "x=-1"
Example 5:
Input: equation = "x=x+2"
Output: "No solution"
Constraints:
3 <= equation.length <= 1000
equation has exactly one '='.
equation consists of integers with an absolute value in the range [0, 100] without any leading zeros, and the variable 'x'.
"""
# V0
# IDEA : replace + eval + math
# https://leetcode.com/problems/solve-the-equation/discuss/105362/Simple-2-liner-(and-more)
# eval : The eval() method parses the expression passed to this method and runs python expression (code) within the program.
# -> https://www.runoob.com/python/python-func-eval.html
class Solution(object):
def solveEquation(self, equation):
tmp = equation.replace('x', 'j').replace('=', '-(')
z = eval(tmp + ")" , {'j':1j})
# print ("equation = " + str(equation))
# print ("tmp = " + str(tmp))
# print ("z = " + str(z))
a, x = z.real, -z.imag
return 'x=%d' % (a / x) if x else 'No solution' if a else 'Infinite solutions'
# V0'
# IDEA : REGULAR EXPRESSION
import re
class Solution(object):
def solveEquation(self, equation):
a, b, side = 0, 0, 1
for eq, sign, num, isx in re.findall('(=)|([-+]?)(\d*)(x?)', equation):
#print ("eq : {}, sign : {}, num : {}, isx : {}".format(eq, sign, num, isx))
# case : "="
if eq:
side = -1
# case : "x"
elif isx:
a += side * int(sign + '1') * int(num or 1)
# case : number
elif num:
b -= side * int(sign + num)
return 'x=%d' % (b / a) if a else 'No solution' if b else 'Infinite solutions'
# V0'
class Solution(object):
def solveEquation(self, equation):
z = eval(equation.replace('x', 'j').replace('=', '-(') + ')', {'j': 1j})
a, x = z.real, -z.imag
return 'x=%d' % (a / x) if x else 'No solution' if a else 'Infinite solutions'
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/80656224
class Solution(object):
def solveEquation(self, equation):
"""
:type equation: str
:rtype: str
"""
left, right = equation.split('=')
if left[0] == '-':
left = '0' + left
if right[0] == '-':
right = '0' + right
left = left.replace('-', '+-')
right = right.replace('-', '+-')
left_x, left_val, right_x, right_val = 0, 0, 0, 0
for num in left.split('+'):
if 'x' in num:
if num == 'x':
left_x += 1
elif num == '-x':
left_x -= 1
else:
left_x += int(num[:-1])
else:
left_val += int(num)
for num in right.split('+'):
if 'x' in num:
if num == 'x':
right_x += 1
elif num == '-x':
right_x -= 1
else:
right_x += int(num[:-1])
else:
right_val += int(num)
left_x -= right_x
right_val -= left_val
if left_x != 0 and right_val == 0:
return "x=0"
elif left_x != 0 and right_val != 0:
return 'x=' + str(right_val / left_x)
elif left_x == 0 and right_val == 0:
return "Infinite solutions"
elif left_x == 0 and right_val != 0:
return "No solution"
### Test case : dev
# V1'
# https://leetcode.com/problems/solve-the-equation/discuss/105362/Simple-2-liner-(and-more)
# IDEA : eval
class Solution(object):
def solveEquation(self, equation):
z = eval(equation.replace('x', 'j').replace('=', '-(') + ')', {'j': 1j})
a, x = z.real, -z.imag
return 'x=%d' % (a / x) if x else 'No solution' if a else 'Infinite solutions'
# V2
# Time: O(n)
# Space: O(n)
import re
class Solution(object):
def solveEquation(self, equation):
"""
:type equation: str
:rtype: str
"""
a, b, side = 0, 0, 1
for eq, sign, num, isx in re.findall('(=)|([-+]?)(\d*)(x?)', equation):
if eq:
side = -1
elif isx:
a += side * int(sign + '1') * int(num or 1)
elif num:
b -= side * int(sign + num)
return 'x=%d' % (b / a) if a else 'No solution' if b else 'Infinite solutions'