Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit e70bcf3

Browse files
committed
972 重构失败
1 parent 0d404b8 commit e70bcf3

File tree

2 files changed

+9
-118
lines changed

2 files changed

+9
-118
lines changed

Algorithms/0972.equal-rational-numbers/equal-rational-numbers.go

Lines changed: 9 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,16 @@ import (
66
)
77

88
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)
1312

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)
1816

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
5219
}
5320

5421
func parse(s string) (string, string, string) {
@@ -66,7 +33,7 @@ func parse(s string) (string, string, string) {
6633
l := strings.Index(fraction, "(")
6734
if l == -1 {
6835
if fraction == "0" {
69-
return integer, "", ""
36+
fraction = ""
7037
}
7138
return integer, fraction, ""
7239
}
@@ -85,34 +52,6 @@ func parse(s string) (string, string, string) {
8552
return integer, nonRepeat, repeat
8653
}
8754

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-
11655
func simplify(nonRepeat, repeat string) (string, string) {
11756
if repeat == "" {
11857
return nonRepeat, repeat
@@ -141,15 +80,6 @@ func simplify(nonRepeat, repeat string) (string, string) {
14180
return nonRepeat, repeat
14281
}
14382

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-
15383
func convert(integer, nonRepeat, repeat string) (int, string) {
15484
i, _ := strconv.Atoi(integer)
15585
for j := len(nonRepeat); j > 0; j-- {

Algorithms/0972.equal-rational-numbers/equal-rational-numbers_test.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -153,42 +153,3 @@ func Test_parse(t *testing.T) {
153153
})
154154
}
155155
}
156-
157-
func Test_normalize(t *testing.T) {
158-
type args struct {
159-
s string
160-
}
161-
tests := []struct {
162-
name string
163-
args args
164-
want string
165-
}{
166-
167-
{
168-
"1",
169-
args{"1"},
170-
"10001",
171-
},
172-
173-
{
174-
"1234.",
175-
args{"1234."},
176-
"11234",
177-
},
178-
179-
{
180-
"1234.(1)",
181-
args{"1234.(1)"},
182-
"11234(1)",
183-
},
184-
185-
// Add test cases.
186-
}
187-
for _, tt := range tests {
188-
t.Run(tt.name, func(t *testing.T) {
189-
if got := normalize(tt.args.s); got != tt.want {
190-
t.Errorf("normalize() = %v, want %v", got, tt.want)
191-
}
192-
})
193-
}
194-
}

0 commit comments

Comments
 (0)