-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolvetheEquation.py
More file actions
36 lines (33 loc) · 1.07 KB
/
solvetheEquation.py
File metadata and controls
36 lines (33 loc) · 1.07 KB
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/solve-the-equation/
# Author: Miao Zhang
# Date: 2021-02-25
class Solution:
def solveEquation(self, equation: str) -> str:
def parse(s: str) -> List[int]:
a = 0
b = 0
num = 0
sign = 1
digit = False
for c in s:
if c.isdigit():
digit = True
num = num * 10 + int(c)
else:
if c == 'x':
a += (num if digit else 1) * sign
else:
b += num * sign
sign = 1 if c == '+' else -1
digit = False
num = 0
b += num * sign
return [a, b]
l, r = parse(equation.split('=')[0]), parse(equation.split('=')[1])
l[0] -= r[0]
r[1] -= l[1]
if l[0] == 0:
return "Infinite solutions" if r[1] == 0 else "No solution"
return "x=" + str(r[1] // l[0])