-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
81 lines (70 loc) · 2.07 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
// Mumble Rap
// https://open.kattis.com/problems/mumblerap
use std::io::stdin;
fn main() {
let mut length = String::new();
stdin()
.read_line(&mut length)
.expect("failed to read stdin");
let length: usize = length.trim().parse().expect("failed to parse length");
let mut input = String::with_capacity(length);
stdin().read_line(&mut input).expect("failed to read stdin");
let res = solution(input);
println!("{res}");
}
fn solution(input: String) -> usize {
let mut scanning_num = false;
let (mut start, mut end, mut max) = (0, 0, 0);
for (i, c) in input.trim().chars().enumerate() {
match c.is_ascii_digit() {
true => match scanning_num {
true => {
end = i;
}
false => {
scanning_num = true;
start = i;
end = i;
}
},
false => match scanning_num {
true => {
scanning_num = false;
let num: usize = input[start..=end].parse().expect("failed to parse slice");
max = max.max(num);
}
false => {}
},
}
}
if scanning_num {
let num: usize = input[start..=end].parse().expect("failed to parse slice");
max = max.max(num);
}
max
}
#[cfg(test)]
mod tests {
use crate::solution;
#[test]
fn sample_one() {
let input = String::from("ihave40dollarsinmybankaccount");
let expected = 40;
let actual = solution(input);
assert_eq!(actual, expected);
}
#[test]
fn sample_two() {
let input = String::from("yesterdayihad1001,BUTnowihave9999");
let expected = 9999;
let actual = solution(input);
assert_eq!(actual, expected);
}
#[test]
fn sample_three() {
let input = String::from("13twenty209sixty123");
let expected = 209;
let actual = solution(input);
assert_eq!(actual, expected);
}
}