This is my attempt at solving this in rust. I'm sure there are a lot of things I could have done better. I appreciate any suggestions for improvement. :)
Thanks!
use std::collections::HashMap;
fn main(){
let mut input = "00000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000".to_string();
for _ in 0..25 {
println!("{}", input);
input = next_step(&input);
}
}
fn get_value(a: &char, b: &char) -> char {
if *a == '0' && *b == '0' {
'0'
} else if *a == '1' && *b == '1' {
'0'
} else if *a == '0' && *b == '1' {
'1'
} else if *a == '1' && *b == '0' {
'1'
} else {
panic!("Should never come here");
}
}
fn next_step(current_step: &String) -> String {
let mut return_val = String::new();
if current_step == "0" || current_step == "1" {
return_val = "0".to_string();
}
if current_step == "00" || current_step == "10" {
return_val = "00".to_string();
} else if current_step == "01" || current_step == "11"{
return_val = "11".to_string();
}
if current_step.len() >= 3 {
return_val = String::new();
let mut iter_a = current_step.chars();
let mut iter_c = current_step.chars();
iter_c.next().expect("value");
let mut current_char = iter_c.next().expect("value");
return_val.push(get_value(&'0', ¤t_char));
for (a1, c1) in iter_a.zip(iter_c) {
return_val.push(get_value(&a1, &c1));
}
return_val.push(get_value(¤t_char, &'0'));
}
return return_val;
}
#[cfg(test)]
mod test {
use super::next_step;
// next step function
// should take in 0 and return 0 because [0]0[0]
#[test]
fn simple_test_for_zero() {
helper_function("0", "0");
}
fn helper_function(input: &str, output: &str) {
let input_string_reference = input.to_string();
let actual = next_step(&input_string_reference);
assert!(actual == output.to_string(), "Expected: {} Actual: {}", output, actual);
}
#[test]
fn simple_test_for_one() {
helper_function("1", "0");
}
/*
* For 00 we should get back 00 because 00 == [0]00 == 0
*/
#[test]
fn simple_test_for_zerozero() {
helper_function("00", "00");
}
// "01" == [0]01 == 11
#[test]
fn simple_test_for_zero_one() {
helper_function("01", "11");
}
// "11" = [0]11 == 11
#[test]
fn simple_test_for_one_one() {
helper_function("11", "11");
}
// "10" = [0]10 == 00
#[test]
fn simple_test_for_one_zero() {
helper_function("10", "00");
}
//"000" = 000
#[test]
fn simple_test_for_zero_zero_zero() {
helper_function("000", "000");
}
//"010" = 000
#[test]
fn simple_test_for_zero_one_zero() {
helper_function("010", "101");
}
#[test]
fn functional_test() {
helper_function("1101010", "1100001");
}
}