-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequalRationalNumbers.py
More file actions
30 lines (26 loc) · 894 Bytes
/
equalRationalNumbers.py
File metadata and controls
30 lines (26 loc) · 894 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/equal-rational-numbers/
# Author: Miao Zhang
# Date: 2021-03-29
class Solution:
def isRationalEqual(self, s: str, t: str) -> bool:
from fractions import Fraction
def convert(x):
if '.' not in x:
return Fraction(int(x), 1)
i = x.index('.')
res = Fraction(int(x[:i]), 1)
x = x[i + 1:]
if '(' not in x:
if x:
res += Fraction(int(x), 10 ** len(x))
return res
i = x.index('(')
if i:
res += Fraction(int(x[: i]), 10 ** i)
x = x[i + 1: -1]
j = len(x)
res += Fraction(int(x), 10 ** i * (10 ** j - 1))
return res
return convert(s) == convert(t)