diff --git a/src/display_information/uniquely_decodable.rs b/src/display_information/uniquely_decodable.rs index b56c0e3..c4884dd 100644 --- a/src/display_information/uniquely_decodable.rs +++ b/src/display_information/uniquely_decodable.rs @@ -6,13 +6,11 @@ /// very closely. use std::collections::HashSet; -/// Return true if the list is uniquely decodable, false if not +/// Return true if the list is uniquely decodable, false if not. I +/// don't _think_ we need to check reversed words in this case. pub fn check_decodability(c: &[String]) -> bool { - let is_c_uniquely_decodable_forwards = sardinas_patterson_theorem(vec_to_hash(c)); - // We need to check both forward and reverse - let c_reversed = reverse_all_words(c); - let is_c_uniquely_decodable_backwards = sardinas_patterson_theorem(vec_to_hash(&c_reversed)); - is_c_uniquely_decodable_forwards || is_c_uniquely_decodable_backwards + let is_c_uniquely_decodable = sardinas_patterson_theorem(vec_to_hash(c)); + is_c_uniquely_decodable } fn vec_to_hash(v: &[String]) -> HashSet { @@ -77,14 +75,6 @@ fn generate_c_infinity_with_a_halt_break(c: HashSet) -> HashSet c_infinity } -fn reverse_all_words(list: &[String]) -> Vec { - let mut reversed_list = vec![]; - for word in list { - reversed_list.push(word.chars().rev().collect::()); - } - reversed_list -} - /// Returns true if c is uniquely decodable fn sardinas_patterson_theorem(c: HashSet) -> bool { let c_infinity = generate_c_infinity_with_a_halt_break(c.clone()); diff --git a/src/list_manipulations.rs b/src/list_manipulations.rs index 8800fc9..d7ba2d5 100644 --- a/src/list_manipulations.rs +++ b/src/list_manipulations.rs @@ -146,9 +146,11 @@ pub fn guarantee_maximum_prefix_length( pub fn schlinkert_prune(list: &[String]) -> Vec { let offenders_to_remove_forwards = get_sardinas_patterson_final_intersection(list); // Reversing all words before running the Schlinkert prune gives a - // different list of offending words - let offenders_to_remove_backwards = - get_sardinas_patterson_final_intersection(&reverse_all_words(list)); + // different list of offending words. (We then have to un-reverse all the + // offending words.) + let offenders_to_remove_backwards = reverse_all_words( + &get_sardinas_patterson_final_intersection(&reverse_all_words(list)), + ); let mut new_list = list.to_owned(); // If running the prune on the reversed words yielded fewer offenders // we'll use that list, since our assumed goal is to remove the fewest