Skip to content

Commit 5947a3f

Browse files
authored
Improve Palindrome (#839)
ref: improve palindrome
1 parent 857c73a commit 5947a3f

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

src/string/palindrome.rs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,74 @@
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`.
115
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+
321
while let (Some(c1), Some(c2)) = (chars.next(), chars.next_back()) {
422
if c1 != c2 {
523
return false;
624
}
725
}
26+
827
true
928
}
1029

1130
#[cfg(test)]
1231
mod tests {
1332
use super::*;
1433

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),
2373
}
2474
}

0 commit comments

Comments
 (0)