|
| 1 | +//! A module for checking if a given string is a palindrome. |
| 2 | +
|
| 3 | +/// Checks if the given string is a palindrome. |
| 4 | +/// |
| 5 | +/// A palindrome is a sequence that reads the same backward as forward. |
| 6 | +/// This function ignores non-alphanumeric characters and is case-insensitive. |
| 7 | +/// |
| 8 | +/// # Arguments |
| 9 | +/// |
| 10 | +/// * `s` - A string slice that represents the input to be checked. |
| 11 | +/// |
| 12 | +/// # Returns |
| 13 | +/// |
| 14 | +/// * `true` if the string is a palindrome; otherwise, `false`. |
1 | 15 | pub fn is_palindrome(s: &str) -> bool {
|
2 |
| - let mut chars = s.chars(); |
| 16 | + let mut chars = s |
| 17 | + .chars() |
| 18 | + .filter(|c| c.is_alphanumeric()) |
| 19 | + .map(|c| c.to_ascii_lowercase()); |
| 20 | + |
3 | 21 | while let (Some(c1), Some(c2)) = (chars.next(), chars.next_back()) {
|
4 | 22 | if c1 != c2 {
|
5 | 23 | return false;
|
6 | 24 | }
|
7 | 25 | }
|
| 26 | + |
8 | 27 | true
|
9 | 28 | }
|
10 | 29 |
|
11 | 30 | #[cfg(test)]
|
12 | 31 | mod tests {
|
13 | 32 | use super::*;
|
14 | 33 |
|
15 |
| - #[test] |
16 |
| - fn palindromes() { |
17 |
| - assert!(is_palindrome("abcba")); |
18 |
| - assert!(is_palindrome("abba")); |
19 |
| - assert!(is_palindrome("a")); |
20 |
| - assert!(is_palindrome("arcra")); |
21 |
| - assert!(!is_palindrome("abcde")); |
22 |
| - assert!(!is_palindrome("aaaabbbb")); |
| 34 | + macro_rules! palindrome_tests { |
| 35 | + ($($name:ident: $inputs:expr,)*) => { |
| 36 | + $( |
| 37 | + #[test] |
| 38 | + fn $name() { |
| 39 | + let (input, expected) = $inputs; |
| 40 | + assert_eq!(is_palindrome(input), expected); |
| 41 | + } |
| 42 | + )* |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + palindrome_tests! { |
| 47 | + odd_palindrome: ("madam", true), |
| 48 | + even_palindrome: ("deified", true), |
| 49 | + single_character_palindrome: ("x", true), |
| 50 | + single_word_palindrome: ("eye", true), |
| 51 | + case_insensitive_palindrome: ("RaceCar", true), |
| 52 | + mixed_case_and_punctuation_palindrome: ("A man, a plan, a canal, Panama!", true), |
| 53 | + mixed_case_and_space_palindrome: ("No 'x' in Nixon", true), |
| 54 | + empty_string: ("", true), |
| 55 | + pompeii_palindrome: ("Roma-Olima-Milo-Amor", true), |
| 56 | + napoleon_palindrome: ("Able was I ere I saw Elba", true), |
| 57 | + john_taylor_palindrome: ("Lewd did I live, & evil I did dwel", true), |
| 58 | + well_know_english_palindrome: ("Never odd or even", true), |
| 59 | + palindromic_phrase: ("Rats live on no evil star", true), |
| 60 | + names_palindrome: ("Hannah", true), |
| 61 | + prime_minister_of_cambodia: ("Lon Nol", true), |
| 62 | + japanese_novelist_and_manga_writer: ("Nisio Isin", true), |
| 63 | + actor: ("Robert Trebor", true), |
| 64 | + rock_vocalist: ("Ola Salo", true), |
| 65 | + pokemon_species: ("Girafarig", true), |
| 66 | + lychrel_num_56: ("121", true), |
| 67 | + universal_palindrome_date: ("02/02/2020", true), |
| 68 | + french_palindrome: ("une Slave valse nu", true), |
| 69 | + finnish_palindrome: ("saippuakivikauppias", true), |
| 70 | + non_palindrome_simple: ("hello", false), |
| 71 | + non_palindrome_with_punctuation: ("hello!", false), |
| 72 | + non_palindrome_mixed_case: ("Hello, World", false), |
23 | 73 | }
|
24 | 74 | }
|
0 commit comments