diff --git a/src/graph/eulerian_path.rs b/src/graph/eulerian_path.rs index 2dcfa8b22a5..d37ee053f43 100644 --- a/src/graph/eulerian_path.rs +++ b/src/graph/eulerian_path.rs @@ -1,101 +1,123 @@ +//! This module provides functionality to find an Eulerian path in a directed graph. +//! An Eulerian path visits every edge exactly once. The algorithm checks if an Eulerian +//! path exists and, if so, constructs and returns the path. + use std::collections::LinkedList; -use std::vec::Vec; - -/// Struct representing an Eulerian Path in a directed graph. -pub struct EulerianPath { - n: usize, // Number of nodes in the graph - edge_count: usize, // Total number of edges in the graph - in_degree: Vec, // In-degrees of nodes - out_degree: Vec, // Out-degrees of nodes - path: LinkedList, // Linked list to store the Eulerian path - graph: Vec>, // Adjacency list representing the directed graph + +/// Finds an Eulerian path in a directed graph. +/// +/// # Arguments +/// +/// * `node_count` - The number of nodes in the graph. +/// * `edge_list` - A vector of tuples representing directed edges, where each tuple is of the form `(start, end)`. +/// +/// # Returns +/// +/// An `Option>` containing the Eulerian path if it exists; otherwise, `None`. +pub fn find_eulerian_path(node_count: usize, edge_list: Vec<(usize, usize)>) -> Option> { + let mut adjacency_list = vec![Vec::new(); node_count]; + for (start, end) in edge_list { + adjacency_list[start].push(end); + } + + let mut eulerian_solver = EulerianPathSolver::new(adjacency_list); + eulerian_solver.find_path() } -impl EulerianPath { - /// Creates a new instance of EulerianPath. +/// Struct to represent the solver for finding an Eulerian path in a directed graph. +pub struct EulerianPathSolver { + node_count: usize, + edge_count: usize, + in_degrees: Vec, + out_degrees: Vec, + eulerian_path: LinkedList, + adjacency_list: Vec>, +} + +impl EulerianPathSolver { + /// Creates a new instance of `EulerianPathSolver`. /// /// # Arguments /// - /// * `graph` - A directed graph represented as an adjacency list. + /// * `adjacency_list` - The graph represented as an adjacency list. /// /// # Returns /// - /// A new EulerianPath instance. - pub fn new(graph: Vec>) -> Self { - let n = graph.len(); + /// A new instance of `EulerianPathSolver`. + pub fn new(adjacency_list: Vec>) -> Self { Self { - n, + node_count: adjacency_list.len(), edge_count: 0, - in_degree: vec![0; n], - out_degree: vec![0; n], - path: LinkedList::new(), - graph, + in_degrees: vec![0; adjacency_list.len()], + out_degrees: vec![0; adjacency_list.len()], + eulerian_path: LinkedList::new(), + adjacency_list, } } - /// Finds an Eulerian path in the directed graph. + /// Find the Eulerian path if it exists. /// /// # Returns /// - /// An `Option` containing the Eulerian path if it exists, or `None` if no Eulerian path exists. - pub fn find_eulerian_path(&mut self) -> Option> { - self.initialize(); + /// An `Option>` containing the Eulerian path if found; otherwise, `None`. + /// + /// If multiple Eulerian paths exist, the one found will be returned, but it may not be unique. + fn find_path(&mut self) -> Option> { + self.initialize_degrees(); if !self.has_eulerian_path() { return None; } - let start_node = self.find_start_node(); - self.traverse(start_node); + let start_node = self.get_start_node(); + self.depth_first_search(start_node); - if self.path.len() != self.edge_count + 1 { + if self.eulerian_path.len() != self.edge_count + 1 { return None; } - let mut solution = Vec::with_capacity(self.edge_count + 1); - while let Some(node) = self.path.pop_front() { - solution.push(node); + let mut path = Vec::with_capacity(self.edge_count + 1); + while let Some(node) = self.eulerian_path.pop_front() { + path.push(node); } - Some(solution) + Some(path) } - /// Initializes the degree vectors and counts the total number of edges in the graph. - fn initialize(&mut self) { - for (from, neighbors) in self.graph.iter().enumerate() { - for &to in neighbors { - self.in_degree[to] += 1; - self.out_degree[from] += 1; + /// Initializes in-degrees and out-degrees for each node and counts total edges. + fn initialize_degrees(&mut self) { + for (start_node, neighbors) in self.adjacency_list.iter().enumerate() { + for &end_node in neighbors { + self.in_degrees[end_node] += 1; + self.out_degrees[start_node] += 1; self.edge_count += 1; } } } - /// Checks if the graph has an Eulerian path. + /// Checks if an Eulerian path exists in the graph. /// /// # Returns /// - /// `true` if an Eulerian path exists, `false` otherwise. + /// `true` if an Eulerian path exists; otherwise, `false`. fn has_eulerian_path(&self) -> bool { if self.edge_count == 0 { return false; } let (mut start_nodes, mut end_nodes) = (0, 0); - for i in 0..self.n { - let in_degree = self.in_degree[i] as i32; - let out_degree = self.out_degree[i] as i32; - - if (out_degree - in_degree) > 1 || (in_degree - out_degree) > 1 { - return false; - } else if (out_degree - in_degree) == 1 { - start_nodes += 1; - } else if (in_degree - out_degree) == 1 { - end_nodes += 1; + for i in 0..self.node_count { + let (in_degree, out_degree) = + (self.in_degrees[i] as isize, self.out_degrees[i] as isize); + match out_degree - in_degree { + 1 => start_nodes += 1, + -1 => end_nodes += 1, + degree_diff if degree_diff.abs() > 1 => return false, + _ => (), } } - (end_nodes == 0 && start_nodes == 0) || (end_nodes == 1 && start_nodes == 1) + (start_nodes == 0 && end_nodes == 0) || (start_nodes == 1 && end_nodes == 1) } /// Finds the starting node for the Eulerian path. @@ -103,31 +125,29 @@ impl EulerianPath { /// # Returns /// /// The index of the starting node. - fn find_start_node(&self) -> usize { - let mut start = 0; - for i in 0..self.n { - if self.out_degree[i] - self.in_degree[i] == 1 { + fn get_start_node(&self) -> usize { + for i in 0..self.node_count { + if self.out_degrees[i] > self.in_degrees[i] { return i; } - if self.out_degree[i] > 0 { - start = i; - } } - start + (0..self.node_count) + .find(|&i| self.out_degrees[i] > 0) + .unwrap_or(0) } - /// Traverses the graph to find the Eulerian path recursively. + /// Performs depth-first search to construct the Eulerian path. /// /// # Arguments /// - /// * `at` - The current node being traversed. - fn traverse(&mut self, at: usize) { - while self.out_degree[at] != 0 { - let next = self.graph[at][self.out_degree[at] - 1]; - self.out_degree[at] -= 1; - self.traverse(next); + /// * `curr_node` - The current node being visited in the DFS traversal. + fn depth_first_search(&mut self, curr_node: usize) { + while self.out_degrees[curr_node] > 0 { + let next_node = self.adjacency_list[curr_node][self.out_degrees[curr_node] - 1]; + self.out_degrees[curr_node] -= 1; + self.depth_first_search(next_node); } - self.path.push_front(at); + self.eulerian_path.push_front(curr_node); } } @@ -135,59 +155,226 @@ impl EulerianPath { mod tests { use super::*; - /// Creates an empty graph with `n` nodes. - fn create_empty_graph(n: usize) -> Vec> { - vec![Vec::new(); n] - } - - /// Adds a directed edge from `from` to `to` in the graph. - fn add_directed_edge(graph: &mut [Vec], from: usize, to: usize) { - graph[from].push(to); - } - - #[test] - fn good_path_test() { - let n = 7; - let mut graph = create_empty_graph(n); - - add_directed_edge(&mut graph, 1, 2); - add_directed_edge(&mut graph, 1, 3); - add_directed_edge(&mut graph, 2, 2); - add_directed_edge(&mut graph, 2, 4); - add_directed_edge(&mut graph, 2, 4); - add_directed_edge(&mut graph, 3, 1); - add_directed_edge(&mut graph, 3, 2); - add_directed_edge(&mut graph, 3, 5); - add_directed_edge(&mut graph, 4, 3); - add_directed_edge(&mut graph, 4, 6); - add_directed_edge(&mut graph, 5, 6); - add_directed_edge(&mut graph, 6, 3); - - let mut solver = EulerianPath::new(graph); - - assert_eq!( - solver.find_eulerian_path().unwrap(), - vec![1, 3, 5, 6, 3, 2, 4, 3, 1, 2, 2, 4, 6] - ); + macro_rules! test_cases { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (n, edges, expected) = $test_case; + assert_eq!(find_eulerian_path(n, edges), expected); + } + )* + } } - #[test] - fn small_path_test() { - let n = 5; - let mut graph = create_empty_graph(n); - - add_directed_edge(&mut graph, 0, 1); - add_directed_edge(&mut graph, 1, 2); - add_directed_edge(&mut graph, 1, 4); - add_directed_edge(&mut graph, 1, 3); - add_directed_edge(&mut graph, 2, 1); - add_directed_edge(&mut graph, 4, 1); - - let mut solver = EulerianPath::new(graph); - - assert_eq!( - solver.find_eulerian_path().unwrap(), - vec![0, 1, 4, 1, 2, 1, 3] - ); + test_cases! { + test_eulerian_cycle: ( + 7, + vec![ + (1, 2), + (1, 3), + (2, 2), + (2, 4), + (2, 4), + (3, 1), + (3, 2), + (3, 5), + (4, 3), + (4, 6), + (5, 6), + (6, 3) + ], + Some(vec![1, 3, 5, 6, 3, 2, 4, 3, 1, 2, 2, 4, 6]) + ), + test_simple_path: ( + 5, + vec![ + (0, 1), + (1, 2), + (1, 4), + (1, 3), + (2, 1), + (4, 1) + ], + Some(vec![0, 1, 4, 1, 2, 1, 3]) + ), + test_disconnected_graph: ( + 4, + vec![ + (0, 1), + (2, 3) + ], + None::> + ), + test_single_cycle: ( + 4, + vec![ + (0, 1), + (1, 2), + (2, 3), + (3, 0) + ], + Some(vec![0, 1, 2, 3, 0]) + ), + test_empty_graph: ( + 3, + vec![], + None::> + ), + test_unbalanced_path: ( + 3, + vec![ + (0, 1), + (1, 2), + (2, 0), + (0, 2) + ], + Some(vec![0, 2, 0, 1, 2]) + ), + test_no_eulerian_path: ( + 3, + vec![ + (0, 1), + (0, 2) + ], + None::> + ), + test_complex_eulerian_path: ( + 6, + vec![ + (0, 1), + (1, 2), + (2, 3), + (3, 4), + (4, 0), + (0, 5), + (5, 0), + (2, 0) + ], + Some(vec![2, 0, 5, 0, 1, 2, 3, 4, 0]) + ), + test_single_node_self_loop: ( + 1, + vec![(0, 0)], + Some(vec![0, 0]) + ), + test_complete_graph: ( + 3, + vec![ + (0, 1), + (0, 2), + (1, 0), + (1, 2), + (2, 0), + (2, 1) + ], + Some(vec![0, 2, 1, 2, 0, 1, 0]) + ), + test_multiple_disconnected_components: ( + 6, + vec![ + (0, 1), + (2, 3), + (4, 5) + ], + None::> + ), + test_unbalanced_graph_with_path: ( + 4, + vec![ + (0, 1), + (1, 2), + (2, 3), + (3, 1) + ], + Some(vec![0, 1, 2, 3, 1]) + ), + test_node_with_no_edges: ( + 4, + vec![ + (0, 1), + (1, 2) + ], + Some(vec![0, 1, 2]) + ), + test_multiple_edges_between_same_nodes: ( + 3, + vec![ + (0, 1), + (1, 2), + (1, 2), + (2, 0) + ], + Some(vec![1, 2, 0, 1, 2]) + ), + test_larger_graph_with_eulerian_path: ( + 10, + vec![ + (0, 1), + (1, 2), + (2, 3), + (3, 4), + (4, 5), + (5, 6), + (6, 7), + (7, 8), + (8, 9), + (9, 0), + (1, 6), + (6, 3), + (3, 8) + ], + Some(vec![1, 6, 3, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8]) + ), + test_no_edges_multiple_nodes: ( + 5, + vec![], + None::> + ), + test_multiple_start_and_end_nodes: ( + 4, + vec![ + (0, 1), + (1, 2), + (2, 0), + (0, 2), + (1, 3) + ], + None::> + ), + test_single_edge: ( + 2, + vec![(0, 1)], + Some(vec![0, 1]) + ), + test_multiple_eulerian_paths: ( + 4, + vec![ + (0, 1), + (1, 2), + (2, 0), + (0, 3), + (3, 0) + ], + Some(vec![0, 3, 0, 1, 2, 0]) + ), + test_dag_path: ( + 4, + vec![ + (0, 1), + (1, 2), + (2, 3) + ], + Some(vec![0, 1, 2, 3]) + ), + test_parallel_edges_case: ( + 2, + vec![ + (0, 1), + (0, 1), + (1, 0) + ], + Some(vec![0, 1, 0, 1]) + ), } } diff --git a/src/graph/mod.rs b/src/graph/mod.rs index fb33cb3c3eb..d4b0b0d00cb 100644 --- a/src/graph/mod.rs +++ b/src/graph/mod.rs @@ -38,7 +38,7 @@ pub use self::detect_cycle::DetectCycle; pub use self::dijkstra::dijkstra; pub use self::dinic_maxflow::DinicMaxFlow; pub use self::disjoint_set_union::DisjointSetUnion; -pub use self::eulerian_path::EulerianPath; +pub use self::eulerian_path::find_eulerian_path; pub use self::floyd_warshall::floyd_warshall; pub use self::ford_fulkerson::ford_fulkerson; pub use self::graph_enumeration::enumerate_graph; diff --git a/src/searching/ternary_search.rs b/src/searching/ternary_search.rs index 8cf975463fc..cb9b5bee477 100644 --- a/src/searching/ternary_search.rs +++ b/src/searching/ternary_search.rs @@ -1,91 +1,195 @@ +//! This module provides an implementation of a ternary search algorithm that +//! works for both ascending and descending ordered arrays. The ternary search +//! function returns the index of the target element if it is found, or `None` +//! if the target is not present in the array. + use std::cmp::Ordering; -pub fn ternary_search( - target: &T, - list: &[T], - mut start: usize, - mut end: usize, -) -> Option { - if list.is_empty() { +/// Performs a ternary search for a specified item within a sorted array. +/// +/// This function can handle both ascending and descending ordered arrays. It +/// takes a reference to the item to search for and a slice of the array. If +/// the item is found, it returns the index of the item within the array. If +/// the item is not found, it returns `None`. +/// +/// # Parameters +/// +/// - `item`: A reference to the item to search for. +/// - `arr`: A slice of the sorted array in which to search. +/// +/// # Returns +/// +/// An `Option` which is: +/// - `Some(index)` if the item is found at the given index. +/// - `None` if the item is not found in the array. +pub fn ternary_search(item: &T, arr: &[T]) -> Option { + if arr.is_empty() { return None; } - while start <= end { - let mid1: usize = start + (end - start) / 3; - let mid2: usize = end - (end - start) / 3; + let is_asc = is_asc_arr(arr); + let mut left = 0; + let mut right = arr.len() - 1; - match target.cmp(&list[mid1]) { - Ordering::Less => end = mid1 - 1, - Ordering::Equal => return Some(mid1), - Ordering::Greater => match target.cmp(&list[mid2]) { - Ordering::Greater => start = mid2 + 1, - Ordering::Equal => return Some(mid2), - Ordering::Less => { - start = mid1 + 1; - end = mid2 - 1; - } - }, + while left <= right { + if match_compare(item, arr, &mut left, &mut right, is_asc) { + return Some(left); } } None } -#[cfg(test)] -mod tests { - use super::*; +/// Compares the item with two middle elements of the current search range and +/// updates the search bounds accordingly. This function handles both ascending +/// and descending ordered arrays. It calculates two middle indices of the +/// current search range and compares the item with the elements at these +/// indices. It then updates the search bounds (`left` and `right`) based on +/// the result of these comparisons. If the item is found, it returns `true`. +/// +/// # Parameters +/// +/// - `item`: A reference to the item to search for. +/// - `arr`: A slice of the array in which to search. +/// - `left`: A mutable reference to the left bound of the search range. +/// - `right`: A mutable reference to the right bound of the search range. +/// - `is_asc`: A boolean indicating whether the array is sorted in ascending order. +/// +/// # Returns +/// +/// A `bool` indicating: +/// - `true` if the item was found in the array. +/// - `false` if the item was not found in the array. +fn match_compare( + item: &T, + arr: &[T], + left: &mut usize, + right: &mut usize, + is_asc: bool, +) -> bool { + let first_mid = *left + (*right - *left) / 3; + let second_mid = *right - (*right - *left) / 3; - #[test] - fn returns_none_if_empty_list() { - let index = ternary_search(&"a", &[], 1, 10); - assert_eq!(index, None); + // Handling the edge case where the search narrows down to a single element + if first_mid == second_mid && first_mid == *left { + return match &arr[*left] { + x if x == item => true, + _ => { + *left += 1; + false + } + }; } - #[test] - fn returns_none_if_range_is_invalid() { - let index = ternary_search(&1, &[1, 2, 3], 2, 1); - assert_eq!(index, None); - } + let cmp_first_mid = item.cmp(&arr[first_mid]); + let cmp_second_mid = item.cmp(&arr[second_mid]); - #[test] - fn returns_index_if_list_has_one_item() { - let index = ternary_search(&1, &[1], 0, 1); - assert_eq!(index, Some(0)); - } - - #[test] - fn returns_first_index() { - let index = ternary_search(&1, &[1, 2, 3], 0, 2); - assert_eq!(index, Some(0)); + match (is_asc, cmp_first_mid, cmp_second_mid) { + // If the item matches either midpoint, it returns the index + (_, Ordering::Equal, _) => { + *left = first_mid; + return true; + } + (_, _, Ordering::Equal) => { + *left = second_mid; + return true; + } + // If the item is smaller than the element at first_mid (in ascending order) + // or greater than it (in descending order), it narrows the search to the first third. + (true, Ordering::Less, _) | (false, Ordering::Greater, _) => { + *right = first_mid.saturating_sub(1) + } + // If the item is greater than the element at second_mid (in ascending order) + // or smaller than it (in descending order), it narrows the search to the last third. + (true, _, Ordering::Greater) | (false, _, Ordering::Less) => *left = second_mid + 1, + // Otherwise, it searches the middle third. + (_, _, _) => { + *left = first_mid + 1; + *right = second_mid - 1; + } } - #[test] - fn returns_first_index_if_end_out_of_bounds() { - let index = ternary_search(&1, &[1, 2, 3], 0, 3); - assert_eq!(index, Some(0)); - } + false +} - #[test] - fn returns_last_index() { - let index = ternary_search(&3, &[1, 2, 3], 0, 2); - assert_eq!(index, Some(2)); - } +/// Determines if the given array is sorted in ascending order. +/// +/// This helper function checks if the first element of the array is less than the +/// last element, indicating an ascending order. It returns `false` if the array +/// has fewer than two elements. +/// +/// # Parameters +/// +/// - `arr`: A slice of the array to check. +/// +/// # Returns +/// +/// A `bool` indicating whether the array is sorted in ascending order. +fn is_asc_arr(arr: &[T]) -> bool { + arr.len() > 1 && arr[0] < arr[arr.len() - 1] +} - #[test] - fn returns_last_index_if_end_out_of_bounds() { - let index = ternary_search(&3, &[1, 2, 3], 0, 3); - assert_eq!(index, Some(2)); - } +#[cfg(test)] +mod tests { + use super::*; - #[test] - fn returns_middle_index() { - let index = ternary_search(&2, &[1, 2, 3], 0, 2); - assert_eq!(index, Some(1)); + macro_rules! test_cases { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (item, arr, expected) = $test_case; + if let Some(expected_index) = expected { + assert_eq!(arr[expected_index], item); + } + assert_eq!(ternary_search(&item, arr), expected); + } + )* + }; } - #[test] - fn returns_middle_index_if_end_out_of_bounds() { - let index = ternary_search(&2, &[1, 2, 3], 0, 3); - assert_eq!(index, Some(1)); + test_cases! { + empty: ("a", &[] as &[&str], None), + one_item_found: ("a", &["a"], Some(0)), + one_item_not_found: ("b", &["a"], None), + search_two_elements_found_at_start: (1, &[1, 2], Some(0)), + search_two_elements_found_at_end: (2, &[1, 2], Some(1)), + search_two_elements_not_found_start: (0, &[1, 2], None), + search_two_elements_not_found_end: (3, &[1, 2], None), + search_three_elements_found_start: (1, &[1, 2, 3], Some(0)), + search_three_elements_found_middle: (2, &[1, 2, 3], Some(1)), + search_three_elements_found_end: (3, &[1, 2, 3], Some(2)), + search_three_elements_not_found_start: (0, &[1, 2, 3], None), + search_three_elements_not_found_end: (4, &[1, 2, 3], None), + search_strings_asc_start: ("a", &["a", "b", "c", "d", "google", "zoo"], Some(0)), + search_strings_asc_middle: ("google", &["a", "b", "c", "d", "google", "zoo"], Some(4)), + search_strings_asc_last: ("zoo", &["a", "b", "c", "d", "google", "zoo"], Some(5)), + search_strings_asc_not_found: ("x", &["a", "b", "c", "d", "google", "zoo"], None), + search_strings_desc_start: ("zoo", &["zoo", "google", "d", "c", "b", "a"], Some(0)), + search_strings_desc_middle: ("google", &["zoo", "google", "d", "c", "b", "a"], Some(1)), + search_strings_desc_last: ("a", &["zoo", "google", "d", "c", "b", "a"], Some(5)), + search_strings_desc_not_found: ("x", &["zoo", "google", "d", "c", "b", "a"], None), + search_ints_asc_start: (1, &[1, 2, 3, 4], Some(0)), + search_ints_asc_middle: (3, &[1, 2, 3, 4], Some(2)), + search_ints_asc_end: (4, &[1, 2, 3, 4], Some(3)), + search_ints_asc_not_found: (5, &[1, 2, 3, 4], None), + search_ints_desc_start: (4, &[4, 3, 2, 1], Some(0)), + search_ints_desc_middle: (3, &[4, 3, 2, 1], Some(1)), + search_ints_desc_end: (1, &[4, 3, 2, 1], Some(3)), + search_ints_desc_not_found: (5, &[4, 3, 2, 1], None), + with_gaps_0: (0, &[1, 3, 8, 11], None), + with_gaps_1: (1, &[1, 3, 8, 11], Some(0)), + with_gaps_2: (2, &[1, 3, 8, 11], None), + with_gaps_3: (3, &[1, 3, 8, 11], Some(1)), + with_gaps_4: (4, &[1, 3, 8, 10], None), + with_gaps_5: (5, &[1, 3, 8, 10], None), + with_gaps_6: (6, &[1, 3, 8, 10], None), + with_gaps_7: (7, &[1, 3, 8, 11], None), + with_gaps_8: (8, &[1, 3, 8, 11], Some(2)), + with_gaps_9: (9, &[1, 3, 8, 11], None), + with_gaps_10: (10, &[1, 3, 8, 11], None), + with_gaps_11: (11, &[1, 3, 8, 11], Some(3)), + with_gaps_12: (12, &[1, 3, 8, 11], None), + with_gaps_13: (13, &[1, 3, 8, 11], None), } } diff --git a/src/string/lipogram.rs b/src/string/lipogram.rs index 93f7d3dce6b..9a486c2a62d 100644 --- a/src/string/lipogram.rs +++ b/src/string/lipogram.rs @@ -1,14 +1,25 @@ use std::collections::HashSet; -/// Function that returns the letters that are missing from the input slice -/// and are present in the English alphabet +/// Represents possible errors that can occur when checking for lipograms. +#[derive(Debug, PartialEq, Eq)] +pub enum LipogramError { + /// Indicates that a non-alphabetic character was found in the input. + NonAlphabeticCharacter, + /// Indicates that a missing character is not in lowercase. + NonLowercaseMissingChar, +} + +/// Computes the set of missing alphabetic characters from the input string. /// -/// ## Arguments +/// # Arguments /// -/// * `in_str` - the slice that will be checked for missing characters +/// * `in_str` - A string slice that contains the input text. /// +/// # Returns +/// +/// Returns a `HashSet` containing the lowercase alphabetic characters that are not present in `in_str`. fn compute_missing(in_str: &str) -> HashSet { - let alphabet: HashSet = "abcdefghijklmnopqrstuvwxyz".chars().collect(); + let alphabet: HashSet = ('a'..='z').collect(); let letters_used: HashSet = in_str .to_lowercase() @@ -19,75 +30,83 @@ fn compute_missing(in_str: &str) -> HashSet { alphabet.difference(&letters_used).cloned().collect() } -/// Function that checks if the slice is a lipogram with specific missing letters. -/// Lipogram - sentence in which a particular letter or group of letters is avoided -/// -/// ## Arguments +/// Checks if the provided string is a lipogram, meaning it is missing specific characters. /// -/// * `lipogram_str` - the slice that will be checked if is a lipogram with specific missing letters -/// * `missing_chars` - the characters that has to be missing +/// # Arguments /// -/// ## Examples +/// * `lipogram_str` - A string slice that contains the text to be checked for being a lipogram. +/// * `missing_chars` - A reference to a `HashSet` containing the expected missing characters. /// -/// ``` -/// use the_algorithms_rust::string::is_lipogram; -/// use std::collections::HashSet; +/// # Returns /// -/// assert!( -/// !is_lipogram("The quick brown fox jumps over the lazy dog", -/// &HashSet::from(['x']) -/// )); -/// -/// assert!( -/// is_lipogram("The brown cat jumped over the lazy dog with a brick", -/// &HashSet::from(['f', 'q', 's', 'x']) -/// )); -/// -/// assert!( -/// !is_lipogram("The quick brown fox jumped over the lazy dog", -/// &HashSet::from(['x']) -/// )); -/// ``` -pub fn is_lipogram(lipogram_str: &str, missing_chars: &HashSet) -> bool { - if !missing_chars.iter().all(|&c| c.is_lowercase()) { - panic!("missing_chars should be all lowercase.") +/// Returns `Ok(true)` if the string is a lipogram that matches the provided missing characters, +/// `Ok(false)` if it does not match, or a `LipogramError` if the input contains invalid characters. +pub fn is_lipogram( + lipogram_str: &str, + missing_chars: &HashSet, +) -> Result { + for &c in missing_chars { + if !c.is_lowercase() { + return Err(LipogramError::NonLowercaseMissingChar); + } } - missing_chars == &compute_missing(lipogram_str) + for c in lipogram_str.chars() { + if !c.is_ascii_alphabetic() && !c.is_whitespace() { + return Err(LipogramError::NonAlphabeticCharacter); + } + } + + let missing = compute_missing(lipogram_str); + Ok(missing == *missing_chars) } #[cfg(test)] mod tests { use super::*; + macro_rules! test_lipogram { - ($($name:ident: $inputs:expr,)*) => { - $( - #[test] - fn $name() { - let (in_str, missing_chars, other_chars) = $inputs; - assert_ne!(missing_chars, other_chars); - assert_eq!(compute_missing(in_str), missing_chars); - assert!(is_lipogram(in_str, &missing_chars)); - assert!(!is_lipogram(in_str, &other_chars)); + ($($name:ident: $tc:expr,)*) => { + $( + #[test] + fn $name() { + let (input, missing_chars, expected) = $tc; + assert_eq!(is_lipogram(input, &missing_chars), expected); + } + )* } - )* } -} test_lipogram! { - lipogram1: ("The quick brown fox jumps over the lazy dog", HashSet::from([]), HashSet::from(['a', 'b'])), - lipogram2: ("Jackdaws love my big sphinx of quartz", HashSet::from([]), HashSet::from(['x'])), - lipogram3: ("abcdefghijklmnopqrstuvwxyz", HashSet::from([]), HashSet::from(['x', 'y', 'z'])), - lipogram4: ("Five quacking zephyrs jolt my wax bed", HashSet::from([]), HashSet::from(['a'])), - lipogram5: ("The quick brown fox jumped over the lazy dog", HashSet::from(['s']), HashSet::from([])), - lipogram6: ("abcdefghijklmnopqrstuvwxy", HashSet::from(['z']), HashSet::from(['y', 'z'])), - lipogram7: ("The brown fox jumped over the lazy dog with a brick", HashSet::from(['q', 's']), HashSet::from(['b'])), - lipogram8: ("ABCdefghijklmnopqrstuvwx", HashSet::from(['y', 'z']), HashSet::from(['a', 'b'])), - } - - #[test] - #[should_panic] - fn test_is_lipogram_panics_when_missing_chars_are_upper_case() { - is_lipogram("abcdefghijklmnopqrstuvwx", &HashSet::from(['y', 'Z'])); + perfect_pangram: ( + "The quick brown fox jumps over the lazy dog", + HashSet::from([]), + Ok(true) + ), + lipogram_single_missing: ( + "The quick brown fox jumped over the lazy dog", + HashSet::from(['s']), + Ok(true) + ), + lipogram_multiple_missing: ( + "The brown fox jumped over the lazy dog", + HashSet::from(['q', 'i', 'c', 'k', 's']), + Ok(true) + ), + long_lipogram_single_missing: ( + "A jovial swain should not complain of any buxom fair who mocks his pain and thinks it gain to quiz his awkward air", + HashSet::from(['e']), + Ok(true) + ), + invalid_non_lowercase_chars: ( + "The quick brown fox jumped over the lazy dog", + HashSet::from(['X']), + Err(LipogramError::NonLowercaseMissingChar) + ), + invalid_non_alphabetic_input: ( + "The quick brown fox jumps over the lazy dog 123@!", + HashSet::from([]), + Err(LipogramError::NonAlphabeticCharacter) + ), } } diff --git a/src/string/reverse.rs b/src/string/reverse.rs index a8e72200787..bf17745a147 100644 --- a/src/string/reverse.rs +++ b/src/string/reverse.rs @@ -1,3 +1,12 @@ +/// Reverses the given string. +/// +/// # Arguments +/// +/// * `text` - A string slice that holds the string to be reversed. +/// +/// # Returns +/// +/// * A new `String` that is the reverse of the input string. pub fn reverse(text: &str) -> String { text.chars().rev().collect() } @@ -6,18 +15,26 @@ pub fn reverse(text: &str) -> String { mod tests { use super::*; - #[test] - fn test_simple() { - assert_eq!(reverse("racecar"), "racecar"); + macro_rules! test_cases { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (input, expected) = $test_case; + assert_eq!(reverse(input), expected); + } + )* + }; } - #[test] - fn test_assymetric() { - assert_eq!(reverse("abcdef"), "fedcba") - } - - #[test] - fn test_sentence() { - assert_eq!(reverse("step on no pets"), "step on no pets"); + test_cases! { + test_simple_palindrome: ("racecar", "racecar"), + test_non_palindrome: ("abcdef", "fedcba"), + test_sentence_with_spaces: ("step on no pets", "step on no pets"), + test_empty_string: ("", ""), + test_single_character: ("a", "a"), + test_leading_trailing_spaces: (" hello ", " olleh "), + test_unicode_characters: ("你好", "好你"), + test_mixed_content: ("a1b2c3!", "!3c2b1a"), } }