-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
150 lines (126 loc) · 3.23 KB
/
main.rs
File metadata and controls
150 lines (126 loc) · 3.23 KB
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
use std::collections::HashMap;
mod exercises;
fn main() {
println!("\n*** Chapter 12 ***\n");
let v = vec![1, 2, 3, 4];
println!("max in {:?} is {}", v, max(&v));
println!(
"fib: the 10th Fibonacci number is {}",
fib(10, &mut HashMap::new())
);
println!("fib_iter: the 10th Fibonacci number is {}", fib_iter(10));
//// Exercises
println!("\n*** Exercises ***\n");
let nums = [100, u8::MAX];
println!(
"sum of {:?} is {:?}",
nums,
exercises::add_until_100(&nums)
);
println!(
"12 appears in the Golomb sequence {} times",
exercises::golomb(12, &mut HashMap::new())
);
println!(
"number of unique paths in 3x7 grid is {}",
exercises::unique_paths(3, 7, &mut HashMap::new())
);
}
fn max(data: &[i32]) -> i32 {
if data.is_empty() {
panic!("pass non-empty data")
}
if data.len() == 1 {
return data[0];
}
let rest_max = max(&data[1..]);
if data[0] > rest_max {
return data[0];
}
rest_max
}
fn fib(n: u8, memo: &mut HashMap<u8, u128>) -> u128 {
if let Some(&val) = memo.get(&n) {
return val;
}
let val = match n {
0 | 1 => n as u128,
_ => fib(n - 2, memo)
.checked_add(fib(n - 1, memo))
.expect("simple examples should not overflow memory"),
};
memo.insert(n, val);
val
}
fn fib_iter(n: u8) -> u128 {
if n == 0 {
return 0;
}
let mut a = 0;
let mut b = 1;
for _ in 1..n {
let tmp: u128 = a;
a = b;
b = tmp
.checked_add(a)
.expect("simple examples should not overflow memory");
}
b
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_max() {
assert_eq!(max(&[1]), 1);
assert_eq!(max(&[1, 2]), 2);
assert_eq!(max(&[2, 1]), 2);
assert_eq!(max(&[2, 1, 3]), 3);
assert_eq!(max(&[2, 3, 1]), 3);
assert_eq!(max(&[3, 2, 1]), 3);
}
#[test]
#[should_panic]
fn test_max_panics() {
max(&[]);
}
#[test]
fn test_fib() {
let mut hm = HashMap::new();
assert_eq!(fib(0, &mut hm), 0);
assert_eq!(fib(1, &mut hm), 1);
assert_eq!(fib(2, &mut hm), 1);
assert_eq!(fib(3, &mut hm), 2);
assert_eq!(fib(4, &mut hm), 3);
assert_eq!(fib(5, &mut hm), 5);
assert_eq!(fib(6, &mut hm), 8);
assert_eq!(fib(7, &mut hm), 13);
assert_eq!(fib(8, &mut hm), 21);
assert_eq!(fib(9, &mut hm), 34);
assert_eq!(fib(10, &mut hm), 55);
}
#[test]
#[should_panic]
fn test_fib_panics() {
fib(u8::MAX, &mut HashMap::new());
}
#[test]
fn test_fib_iter() {
assert_eq!(fib_iter(0), 0);
assert_eq!(fib_iter(1), 1);
assert_eq!(fib_iter(2), 1);
assert_eq!(fib_iter(3), 2);
assert_eq!(fib_iter(4), 3);
assert_eq!(fib_iter(5), 5);
assert_eq!(fib_iter(6), 8);
assert_eq!(fib_iter(7), 13);
assert_eq!(fib_iter(8), 21);
assert_eq!(fib_iter(9), 34);
assert_eq!(fib_iter(10), 55);
}
#[test]
#[should_panic]
fn test_fib_iter_panics() {
fib_iter(u8::MAX);
}
}