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

Commit 5bdcdc7

Browse files
committed
972 refract
1 parent 123a6cb commit 5bdcdc7

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ func isRationalEqual(S string, T string) bool {
1414
tn, tr = simplify(tn, tr)
1515
t, tr := convert(ti, tn, tr)
1616

17-
return s == t &&
18-
sr == tr
17+
return s == t && sr == tr
18+
}
19+
20+
// 1.2(3) -> 0.00012(3) -> 00012(3)
21+
func normalize(s string) string {
22+
if !strings.Contains(s, ".") {
23+
s += "."
24+
}
25+
dot := strings.Index(s, ".")
26+
if dot < 4 {
27+
s = strings.Repeat("0", 4-dot) + s
28+
}
29+
return strings.Replace(s, ".", "", 1)
1930
}
2031

2132
func parse(s string) (string, string, string) {
@@ -33,7 +44,7 @@ func parse(s string) (string, string, string) {
3344
l := strings.Index(fraction, "(")
3445
if l == -1 {
3546
if fraction == "0" {
36-
fraction = ""
47+
return integer, "", ""
3748
}
3849
return integer, fraction, ""
3950
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,42 @@ 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+
"0001",
171+
},
172+
173+
{
174+
"1234.",
175+
args{"1234."},
176+
"1234",
177+
},
178+
179+
{
180+
"1234.(1)",
181+
args{"1234.(1)"},
182+
"1234(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)