Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git i/src/rust/phone_encoder/src/main.rs w/src/rust/phone_encoder/src/main.rs
- index eb67aa8..4a34204 100644
- --- i/src/rust/phone_encoder/src/main.rs
- +++ w/src/rust/phone_encoder/src/main.rs
- @@ -28,10 +28,9 @@ fn main() -> io::Result<()> {
- for line in read_lines(input_file)? {
- if let Ok(num) = line {
- - let digits: Vec<_> = num.chars()
- - .filter(|ch| ch.is_alphanumeric())
- - .collect();
- - print_translations(&num, &digits, 0, Vec::new(), &dict)?;
- + let mut words = Vec::new();
- + let digits: Vec<_> = num.chars().filter(|ch| ch.is_alphanumeric()).collect();
- + print_translations(&num, &digits, 0, &mut words, &dict)?;
- }
- }
- Ok(())
- @@ -41,7 +40,7 @@ fn print_translations(
- num: &str,
- digits: &Vec<char>,
- start: usize,
- - words: Vec<&String>,
- + words: &mut Vec<String>,
- dict: &Dictionary,
- ) -> io::Result<()> {
- if start >= digits.len() {
- @@ -55,23 +54,22 @@ fn print_translations(
- if let Some(found_words) = dict.get(&n) {
- for word in found_words {
- found_word = true;
- - let mut partial_solution = words.clone();
- - partial_solution.push(word);
- - print_translations(num, digits, i + 1, partial_solution, dict)?;
- + words.push(word.clone());
- + print_translations(num, digits, i + 1, words, dict)?;
- + words.pop();
- }
- }
- }
- if !found_word && !words.last().map(|w| is_digit(w)).unwrap_or(false) {
- - let mut partial_solution = words.clone();
- let digit = nth_digit(digits, start).to_string();
- - partial_solution.push(&digit);
- - print_translations(num, digits, start + 1, partial_solution, dict)
- - } else {
- - Ok(())
- + words.push(digit);
- + print_translations(num, digits, start + 1, words, dict)?;
- + words.pop();
- }
- + Ok(())
- }
- -fn print_solution(num: &str, words: &Vec<&String>) {
- +fn print_solution(num: &str, words: &Vec<String>) {
- // do a little gymnastics here to avoid allocating a big string just for printing it
- print!("{}", num);
- if words.is_empty() {
- @@ -142,4 +140,4 @@ fn char_to_digit(ch: char) -> u32 {
- 'g' | 'h' | 'z' => 9,
- _ => panic!("invalid input: not a digit: {}", ch)
- }
- -}
- \ No newline at end of file
- +}
Advertisement
Add Comment
Please, Sign In to add comment