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

Commit d0a7500

Browse files
committed
972 test pass
1 parent 4c530c5 commit d0a7500

File tree

2 files changed

+100
-23
lines changed

2 files changed

+100
-23
lines changed

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,16 @@ import (
66
)
77

88
func isRationalEqual(S string, T string) bool {
9-
is, ns, rs := parse(S)
10-
it, nt, rt := parse(T)
9+
si, sn, sr := parse(S)
10+
sn, sr = simplify(sn, sr)
11+
s, sr := convert(si, sn, sr)
1112

12-
// if rs == "" && rt == "" {
13-
// return is == it && ns == nt
14-
// }
15-
16-
ns, rs = simplify(ns, rs)
17-
nt, rt = simplify(nt, rt)
18-
19-
s, ns, rs := convert(is, ns, rs)
20-
t, nt, rt := convert(it, nt, rt)
13+
ti, tn, tr := parse(T)
14+
tn, tr = simplify(tn, tr)
15+
t, tr := convert(ti, tn, tr)
2116

2217
return s == t &&
23-
ns == nt &&
24-
rs == rt
18+
sr == tr
2519
}
2620

2721
func parse(s string) (string, string, string) {
@@ -31,13 +25,16 @@ func parse(s string) (string, string, string) {
3125

3226
dot := strings.Index(s, ".")
3327
if dot == -1 {
34-
return s, "0", ""
28+
return s, "", ""
3529
}
3630

3731
integer, fraction := s[:dot], s[dot+1:]
3832

3933
l := strings.Index(fraction, "(")
4034
if l == -1 {
35+
if fraction == "0" {
36+
fraction = ""
37+
}
4138
return integer, fraction, ""
4239
}
4340

@@ -48,8 +45,8 @@ func parse(s string) (string, string, string) {
4845
repeat = ""
4946
}
5047

51-
if repeat == "" && nonRepeat == "" {
52-
nonRepeat = "0"
48+
if repeat == "" && nonRepeat == "0" {
49+
nonRepeat = ""
5350
}
5451

5552
return integer, nonRepeat, repeat
@@ -71,20 +68,26 @@ func simplify(nonRepeat, repeat string) (string, string) {
7168
for i := 1; i < len(repeat); i++ {
7269
if strings.HasSuffix(nonRepeat, repeat[i:]) {
7370
repeat = repeat[i:] + repeat[:i]
74-
nonRepeat = nonRepeat[:len(nonRepeat)-i]
71+
nonRepeat = nonRepeat[:len(nonRepeat)-len(repeat)+i]
7572
break
7673
}
7774
}
7875

7976
return nonRepeat, repeat
8077
}
8178

82-
func convert(integer, nonRepeat, repeat string) (int, string, string) {
79+
func convert(integer, nonRepeat, repeat string) (int, string) {
8380
i, _ := strconv.Atoi(integer)
84-
if nonRepeat == "" && repeat == "9" {
81+
for j := len(nonRepeat); j > 0; j-- {
82+
i *= 10
83+
}
84+
if nonRepeat != "" {
85+
n, _ := strconv.Atoi(nonRepeat)
86+
i += n
87+
}
88+
if repeat == "9" {
8589
i++
8690
repeat = ""
87-
nonRepeat = "0"
8891
}
89-
return i, nonRepeat, repeat
92+
return i, repeat
9093
}

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

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ var tcs = []struct {
1414
}{
1515

1616
{
17-
"1.9(0)",
18-
"1.8(9)",
17+
"15.(9)",
18+
"16",
19+
true,
20+
},
21+
22+
{
23+
"1.0(9)",
24+
"1.1",
1925
true,
2026
},
2127

@@ -25,6 +31,12 @@ var tcs = []struct {
2531
true,
2632
},
2733

34+
{
35+
"1.9(0)",
36+
"1.8(9)",
37+
true,
38+
},
39+
2840
{
2941
"0",
3042
"0.",
@@ -73,3 +85,65 @@ func Benchmark_isRationalEqual(b *testing.B) {
7385
}
7486
}
7587
}
88+
89+
func Test_parse(t *testing.T) {
90+
type args struct {
91+
s string
92+
}
93+
tests := []struct {
94+
name string
95+
args args
96+
integer string
97+
nonRep string
98+
repeat string
99+
}{
100+
101+
{
102+
"1",
103+
args{"1"},
104+
"1",
105+
"",
106+
"",
107+
},
108+
109+
{
110+
"1.",
111+
args{"1."},
112+
"1",
113+
"",
114+
"",
115+
},
116+
117+
{
118+
"1.0",
119+
args{"1.0"},
120+
"1",
121+
"",
122+
"",
123+
},
124+
125+
{
126+
"1.0(9)",
127+
args{"1.0(9)"},
128+
"1",
129+
"0",
130+
"9",
131+
},
132+
133+
//
134+
}
135+
for _, tt := range tests {
136+
t.Run(tt.name, func(t *testing.T) {
137+
integer, nonRep, repeat := parse(tt.args.s)
138+
if integer != tt.integer {
139+
t.Errorf("parse() integer = %v, want %v", integer, tt.integer)
140+
}
141+
if nonRep != tt.nonRep {
142+
t.Errorf("parse() nonRepeat = %v, want %v", nonRep, tt.nonRep)
143+
}
144+
if repeat != tt.repeat {
145+
t.Errorf("parse() repeat = %v, want %v", repeat, tt.repeat)
146+
}
147+
})
148+
}
149+
}

0 commit comments

Comments
 (0)