@@ -6,49 +6,16 @@ import (
6
6
)
7
7
8
8
func isRationalEqual (S string , T string ) bool {
9
- S = normalize (S )
10
- sn , sr := parse2 (S )
11
- sn , sr = simplify2 (sn , sr )
12
- s , sr := convert2 (sn , sr )
9
+ si , sn , sr := parse (S )
10
+ sn , sr = simplify (sn , sr )
11
+ s , sr := convert (si , sn , sr )
13
12
14
- T = normalize (T )
15
- tn , tr := parse2 (T )
16
- tn , tr = simplify2 (tn , tr )
17
- t , tr := convert2 (tn , tr )
13
+ ti , tn , tr := parse (T )
14
+ tn , tr = simplify (tn , tr )
15
+ t , tr := convert (ti , tn , tr )
18
16
19
- return s == t && sr == tr
20
- }
21
-
22
- // 1.2(3) -> 0.00012(3) -> 00012(3)
23
- func normalize (s string ) string {
24
- if ! strings .Contains (s , "." ) {
25
- s += "."
26
- }
27
- if strings .HasSuffix (s , "." ) {
28
- s += "0"
29
- }
30
- dot := strings .Index (s , "." )
31
- if dot < 4 {
32
- s = strings .Repeat ("0" , 4 - dot ) + s
33
- }
34
- return strings .Replace ("1" + s , "." , "" , 1 )
35
- }
36
-
37
- func parse2 (s string ) (string , string ) {
38
- if ! strings .Contains (s , "(" ) {
39
- return s , ""
40
- }
41
-
42
- i := strings .Index (s , "(" )
43
-
44
- nonRepeat := s [:i ]
45
- repeat := s [i + 1 : len (s )- 1 ]
46
-
47
- if repeat == "0" {
48
- repeat = ""
49
- }
50
-
51
- return nonRepeat , repeat
17
+ return s == t &&
18
+ sr == tr
52
19
}
53
20
54
21
func parse (s string ) (string , string , string ) {
@@ -66,7 +33,7 @@ func parse(s string) (string, string, string) {
66
33
l := strings .Index (fraction , "(" )
67
34
if l == - 1 {
68
35
if fraction == "0" {
69
- return integer , "" , ""
36
+ fraction = ""
70
37
}
71
38
return integer , fraction , ""
72
39
}
@@ -85,34 +52,6 @@ func parse(s string) (string, string, string) {
85
52
return integer , nonRepeat , repeat
86
53
}
87
54
88
- func simplify2 (nonRepeat , repeat string ) (string , string ) {
89
- if repeat == "" {
90
- return nonRepeat , repeat
91
- }
92
-
93
- if repeat == strings .Repeat (repeat [:1 ], len (repeat )) {
94
- repeat = repeat [:1 ]
95
- }
96
-
97
- for repeat [:len (repeat )/ 2 ] == repeat [len (repeat )/ 2 :] {
98
- repeat = repeat [:len (repeat )/ 2 ]
99
- }
100
-
101
- for strings .HasSuffix (nonRepeat , repeat ) {
102
- nonRepeat = nonRepeat [:len (nonRepeat )- len (repeat )]
103
- }
104
-
105
- for i := 1 ; i < len (repeat ); i ++ {
106
- if strings .HasSuffix (nonRepeat , repeat [i :]) {
107
- repeat = repeat [i :] + repeat [:i ]
108
- nonRepeat = nonRepeat [:len (nonRepeat )- len (repeat )+ i ]
109
- break
110
- }
111
- }
112
-
113
- return nonRepeat , repeat
114
- }
115
-
116
55
func simplify (nonRepeat , repeat string ) (string , string ) {
117
56
if repeat == "" {
118
57
return nonRepeat , repeat
@@ -141,15 +80,6 @@ func simplify(nonRepeat, repeat string) (string, string) {
141
80
return nonRepeat , repeat
142
81
}
143
82
144
- func convert2 (nonRepeat , repeat string ) (int , string ) {
145
- i , _ := strconv .Atoi (nonRepeat )
146
- if repeat == "9" {
147
- i ++
148
- repeat = ""
149
- }
150
- return i , repeat
151
- }
152
-
153
83
func convert (integer , nonRepeat , repeat string ) (int , string ) {
154
84
i , _ := strconv .Atoi (integer )
155
85
for j := len (nonRepeat ); j > 0 ; j -- {
0 commit comments