Skip to content

Commit c17cc27

Browse files
author
openset
committed
Add: Valid Number
1 parent 54eb30d commit c17cc27

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

problems/valid-number/valid_number.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
package valid_number
1+
package problem_65
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
func isNumber(s string) bool {
9+
s = strings.TrimSpace(s)
10+
_, err := strconv.ParseFloat(s, 64)
11+
return err == nil || err.(*strconv.NumError).Err != strconv.ErrSyntax
12+
}
Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,79 @@
1-
package valid_number
1+
package problem_65
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input string
7+
expected bool
8+
}
9+
10+
func TestIsNumber(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: "0",
14+
expected: true,
15+
},
16+
{
17+
input: " 0.1",
18+
expected: true,
19+
},
20+
{
21+
input: "abc",
22+
expected: false,
23+
},
24+
{
25+
input: "1 a",
26+
expected: false,
27+
},
28+
{
29+
input: "2e10",
30+
expected: true,
31+
},
32+
{
33+
input: " -90e3 ",
34+
expected: true,
35+
},
36+
{
37+
input: " 1e",
38+
expected: false,
39+
},
40+
{
41+
input: "e3",
42+
expected: false,
43+
},
44+
{
45+
input: " 6e-1",
46+
expected: true,
47+
},
48+
{
49+
input: " 99e2.5 ",
50+
expected: false,
51+
},
52+
{
53+
input: "53.5e93",
54+
expected: true,
55+
},
56+
{
57+
input: " --6 ",
58+
expected: false,
59+
},
60+
{
61+
input: "-+3",
62+
expected: false,
63+
},
64+
{
65+
input: "95a54e53",
66+
expected: false,
67+
},
68+
{
69+
input: "078332e437",
70+
expected: true,
71+
},
72+
}
73+
for _, tc := range tests {
74+
output := isNumber(tc.input)
75+
if output != tc.expected {
76+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)