Guest User

Untitled

a guest
Aug 6th, 2021
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.44 KB | None | 0 0
  1. diff --git i/src/rust/phone_encoder/src/main.rs w/src/rust/phone_encoder/src/main.rs
  2. index eb67aa8..4a34204 100644
  3. --- i/src/rust/phone_encoder/src/main.rs
  4. +++ w/src/rust/phone_encoder/src/main.rs
  5. @@ -28,10 +28,9 @@ fn main() -> io::Result<()> {
  6.  
  7.      for line in read_lines(input_file)? {
  8.          if let Ok(num) = line {
  9. -            let digits: Vec<_> = num.chars()
  10. -                .filter(|ch| ch.is_alphanumeric())
  11. -                .collect();
  12. -            print_translations(&num, &digits, 0, Vec::new(), &dict)?;
  13. +            let mut words = Vec::new();
  14. +            let digits: Vec<_> = num.chars().filter(|ch| ch.is_alphanumeric()).collect();
  15. +            print_translations(&num, &digits, 0, &mut words, &dict)?;
  16.          }
  17.      }
  18.      Ok(())
  19. @@ -41,7 +40,7 @@ fn print_translations(
  20.      num: &str,
  21.      digits: &Vec<char>,
  22.      start: usize,
  23. -    words: Vec<&String>,
  24. +    words: &mut Vec<String>,
  25.      dict: &Dictionary,
  26.  ) -> io::Result<()> {
  27.      if start >= digits.len() {
  28. @@ -55,23 +54,22 @@ fn print_translations(
  29.          if let Some(found_words) = dict.get(&n) {
  30.              for word in found_words {
  31.                  found_word = true;
  32. -                let mut partial_solution = words.clone();
  33. -                partial_solution.push(word);
  34. -                print_translations(num, digits, i + 1, partial_solution, dict)?;
  35. +                words.push(word.clone());
  36. +                print_translations(num, digits, i + 1, words, dict)?;
  37. +                words.pop();
  38.              }
  39.          }
  40.      }
  41.      if !found_word && !words.last().map(|w| is_digit(w)).unwrap_or(false) {
  42. -        let mut partial_solution = words.clone();
  43.          let digit = nth_digit(digits, start).to_string();
  44. -        partial_solution.push(&digit);
  45. -        print_translations(num, digits, start + 1, partial_solution, dict)
  46. -    } else {
  47. -        Ok(())
  48. +        words.push(digit);
  49. +        print_translations(num, digits, start + 1, words, dict)?;
  50. +        words.pop();
  51.      }
  52. +    Ok(())
  53.  }
  54.  
  55. -fn print_solution(num: &str, words: &Vec<&String>) {
  56. +fn print_solution(num: &str, words: &Vec<String>) {
  57.      // do a little gymnastics here to avoid allocating a big string just for printing it
  58.      print!("{}", num);
  59.      if words.is_empty() {
  60. @@ -142,4 +140,4 @@ fn char_to_digit(ch: char) -> u32 {
  61.          'g' | 'h' | 'z' => 9,
  62.          _ => panic!("invalid input: not a digit: {}", ch)
  63.      }
  64. -}
  65. \ No newline at end of file
  66. +}
Advertisement
Add Comment
Please, Sign In to add comment