-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
168 lines (154 loc) · 7.05 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use regex::Regex;
fn main() {
assert_eq!(Solution::is_number(String::from("0")), true);
}
struct Solution {}
#[allow(dead_code)]
impl Solution {
pub fn is_number_(s: String) -> bool {
let s = s.trim();
if s.chars().any(|x| !x.is_digit(10) && !['e', '+', '-', '.'].contains(&x)) {
return false
}
if s.contains('e') {
let v: Vec<&str> = s.split('e').collect();
if v.len() != 2 { return false }
return Self::str_is_number(v[0]) && Self::str_is_number(v[1]) &&
v[1].chars().all(|x| !['e', '.'].contains(&x))
}
Self::str_is_number(s)
}
pub fn str_is_number(s: &str) -> bool {
if s.len() == 0 { return false }
let mut sg = '1';
let (sgn, dot) = s.chars().fold((0, 0), |mut sum, x| {
if ['+', '-'].contains(&x) { sum.0 += 1; sg = x }
if x == '.' { sum.1 += 1 }
sum
});
if sgn > 1 || dot > 1 { return false }
if sgn == 1 {
if s.find(sg).unwrap() != 0 || s.len() == 1 { return false }
return Self::str_is_number(&s[1..])
}
if dot == 1 && s.find('.').unwrap() == 0 { return s.len() > 1 }
true
}
pub fn is_number_re(s: String) -> bool {
// this regex works
let re = Regex::new(r"^(\+|-)?(\d+(\.\d*)?|\.\d+)(e(\+|-)?\d+)?$").unwrap();
println!("s = {:#?}", s);
re.is_match(s.trim())
}
pub fn is_number_dfa(s: String) -> bool {
let mut state = S1;
for c in s.trim().chars() {
match state {
S1 => {
match c {
'+' | '-' => state = S2,
'.' => state = S3,
_ => if "0123456789".contains(c) { state = S4 } else { return false },
}
}
S2 => {
match c {
'.' => state = S3,
_ => if "0123456789".contains(c) { state = S4 } else { return false },
}
}
S3 => if "0123456789".contains(c) { state = S5 } else { return false }
S4 => {
match c {
'.' => state = S5,
'e' => state = S6,
_ => if "0123456789".contains(c) { state = S4 } else { return false },
}
}
S5 => {
match c {
'e' => state = S6,
_ => if "0123456789".contains(c) { state = S5 } else { return false },
}
}
S6 => {
match c {
'+' | '-' => state = S7,
_ => if "0123456789".contains(c) { state = S8 } else { return false },
}
}
S7 => if "0123456789".contains(c) { state = S8 } else { return false }
S8 => if "0123456789".contains(c) { state = S8 } else { return false }
}
}
// println!("s = {:#?}, state = {:#?}", s, state);
if state == S4 || state == S5 || state == S8 { true } else { false }
}
pub fn is_number(s: String) -> bool { // sneaky way but you **should** think of
s.trim().parse::<f64>().is_ok()
}
}
#[derive(PartialEq, Debug)]
enum State {S1, S2, S3, S4, S5, S6, S7, S8}
use State::{S1, S2, S3, S4, S5, S6, S7, S8};
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::is_number(String::from("0")), true);
assert_eq!(Solution::is_number(String::from("0.1")), true);
assert_eq!(Solution::is_number(String::from("2e10")), true);
assert_eq!(Solution::is_number(String::from("e3")), false);
assert_eq!(Solution::is_number(String::from("abc")), false);
assert_eq!(Solution::is_number(String::from("1 a")), false);
assert_eq!(Solution::is_number(String::from(" 99e2.5 ")), false);
assert_eq!(Solution::is_number(String::from(" +-3")), false);
assert_eq!(Solution::is_number(String::from("95a54e53")), false);
assert_eq!(Solution::is_number(String::from(" --6 ")), false);
assert_eq!(Solution::is_number(String::from(" .8+ ")), false);
assert_eq!(Solution::is_number(String::from(" 4e+ ")), false);
}
#[test]
fn fail() {
assert_eq!(Solution::is_number(String::from(" -. ")), false);
assert_eq!(Solution::is_number(String::from(" -90e3 ")), true);
assert_eq!(Solution::is_number(String::from(" ... ")), false);
assert_eq!(Solution::is_number(String::from(" . ")), false);
assert_eq!(Solution::is_number(String::from(" 5+1 ")), false);
assert_eq!(Solution::is_number(String::from(" 6e-1")), true);
assert_eq!(Solution::is_number(String::from("53.5e93")), true);
assert_eq!(Solution::is_number(String::from(" 1e")), false);
}
#[test]
fn leetcode() {
assert_eq!(Solution::is_number(String::from("123")), true);
assert_eq!(Solution::is_number(String::from(" 123 ")), true);
assert_eq!(Solution::is_number(String::from("0")), true);
assert_eq!(Solution::is_number(String::from("0123")), true); //Cannot agree
assert_eq!(Solution::is_number(String::from("00")), true); //Cannot agree
assert_eq!(Solution::is_number(String::from("-10")), true);
assert_eq!(Solution::is_number(String::from("-0")), true);
assert_eq!(Solution::is_number(String::from("123.5")), true);
assert_eq!(Solution::is_number(String::from("123.000000")), true);
assert_eq!(Solution::is_number(String::from("-500.777")), true);
assert_eq!(Solution::is_number(String::from("0.0000001")), true);
assert_eq!(Solution::is_number(String::from("0.00000")), true);
assert_eq!(Solution::is_number(String::from("0.")), true); //Cannot be more disagree!!!
assert_eq!(Solution::is_number(String::from("00.5")), true); //Strongly cannot agree
assert_eq!(Solution::is_number(String::from("123e1")), true);
assert_eq!(Solution::is_number(String::from("1.23e10")), true);
assert_eq!(Solution::is_number(String::from("0.5e-10")), true);
assert_eq!(Solution::is_number(String::from("1.0e4.5")), false);
assert_eq!(Solution::is_number(String::from("0.5e04")), true);
assert_eq!(Solution::is_number(String::from("12 3")), false);
assert_eq!(Solution::is_number(String::from("1a3")), false);
assert_eq!(Solution::is_number(String::from("")), false);
assert_eq!(Solution::is_number(String::from(" ")), false);
assert_eq!(Solution::is_number(String::from(".1")), true); //Ok)), if you say so
assert_eq!(Solution::is_number(String::from(".")), false);
assert_eq!(Solution::is_number(String::from("2e0")), true); //Really?!
assert_eq!(Solution::is_number(String::from("+.8")), true);
assert_eq!(Solution::is_number(String::from(" 005047e+6")), true); //Damn = =|||
}
}