-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathn_bits_gray_code.rs
75 lines (68 loc) · 1.85 KB
/
n_bits_gray_code.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
/// Custom error type for Gray code generation.
#[derive(Debug, PartialEq)]
pub enum GrayCodeError {
ZeroBitCount,
}
/// Generates an n-bit Gray code sequence using the direct Gray code formula.
///
/// # Arguments
///
/// * `n` - The number of bits for the Gray code.
///
/// # Returns
///
/// A vector of Gray code sequences as strings.
pub fn generate_gray_code(n: usize) -> Result<Vec<String>, GrayCodeError> {
if n == 0 {
return Err(GrayCodeError::ZeroBitCount);
}
let num_codes = 1 << n;
let mut result = Vec::with_capacity(num_codes);
for i in 0..num_codes {
let gray = i ^ (i >> 1);
let gray_code = (0..n)
.rev()
.map(|bit| if gray & (1 << bit) != 0 { '1' } else { '0' })
.collect::<String>();
result.push(gray_code);
}
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! gray_code_tests {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (input, expected) = $test_case;
assert_eq!(generate_gray_code(input), expected);
}
)*
};
}
gray_code_tests! {
zero_bit_count: (0, Err(GrayCodeError::ZeroBitCount)),
gray_code_1_bit: (1, Ok(vec![
"0".to_string(),
"1".to_string(),
])),
gray_code_2_bit: (2, Ok(vec![
"00".to_string(),
"01".to_string(),
"11".to_string(),
"10".to_string(),
])),
gray_code_3_bit: (3, Ok(vec![
"000".to_string(),
"001".to_string(),
"011".to_string(),
"010".to_string(),
"110".to_string(),
"111".to_string(),
"101".to_string(),
"100".to_string(),
])),
}
}