-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-1.rs
57 lines (51 loc) · 1.08 KB
/
ch-1.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
fn main() {
let ints = vec![1, 2, 3, 4, 5];
let ints2 = vec![2, 3, 8, 16];
let ints3 = vec![1, 3, 5, 7, 9];
proc(ints);
proc(ints2);
proc(ints3);
}
fn truth(ints : Vec<i32>) -> bool {
let N :u32 = ints.len() as u32;
for i in 0 .. 2_i32.pow(N) - 1 {
let d = format!("{:b}",i);
let mut D : Vec<&str> = d.split("").collect();
D.pop();
D.reverse();
D.pop();
D.reverse();
for _j in 0 .. N - (d.len() as u32) {
D.insert(0, "0");
}
let mut current_list : Vec<i32> = Vec::new();
for j in 0 .. D.len() {
if "1" == D[j] {
current_list.push(ints[j]);
}
}
if 1 >= current_list.len() {
continue;
}
let mut tally = 0;
for c in ¤t_list {
tally = tally | c;
}
let x = format!("{:b}", tally);
let last_bin_dig = x.chars().last().unwrap();
if last_bin_dig == '0' {
print!("\t{} <--> {:?} ", x, current_list);
return true;
}
}
return false;
}
fn proc(ints : Vec<i32>) {
println!("Input: ints = {:?}", ints);
print!("Output: ");
if truth(ints) {
println!("true");
} else {
println!("false");
}
}