Created
June 13, 2020 22:00
-
-
Save BrianKopp/6c5e9211dfb1f73353a5de4c0aa1af24 to your computer and use it in GitHub Desktop.
Programming Challenge - Rust - Roman Numerals Checkpoint 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// associate a roman numeral with its value | |
struct RomanNumeral { | |
txt: String, | |
value: i32, | |
} | |
fn is_factor_of_ten(i: i32) -> bool { | |
if i == 1 { | |
return true; | |
} | |
let div_ten = i / 10; | |
// values will floor to zero if they're not a factor of 10 | |
if div_ten == 0 { | |
return false; | |
} | |
return is_factor_of_ten(div_ten); | |
} | |
fn subtractor_value(desc_numerals: &Vec<RomanNumeral>, current_pos: usize) -> Option<RomanNumeral> { | |
let curr = desc_numerals.get(current_pos).unwrap(); | |
let mut next_numeral = String::new(); | |
match desc_numerals.get(current_pos + 1) { | |
None => { | |
return None; | |
}, | |
Some(next_rn) => { | |
if is_factor_of_ten(next_rn.value) { | |
next_numeral.push_str(&next_rn.txt); | |
next_numeral.push_str(&curr.txt); | |
return Some(RomanNumeral{ | |
txt: next_numeral, | |
value: curr.value - next_rn.value | |
}); | |
} | |
match desc_numerals.get(current_pos + 2) { | |
None => None, | |
Some(next2_rn) => { | |
if is_factor_of_ten(next2_rn.value) { | |
next_numeral.push_str(&next2_rn.txt); | |
next_numeral.push_str(&curr.txt); | |
return Some(RomanNumeral{ | |
txt: next_numeral, | |
value: curr.value - next2_rn.value | |
}); | |
} | |
return None; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment