Solve palindrome-number.
This commit is contained in:
parent
df1fa87bb3
commit
c8390c584f
|
@ -1,2 +1,3 @@
|
|||
pub mod palindrome_number;
|
||||
pub mod reverse_integer;
|
||||
pub mod two_sum;
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
pub fn is_palindrome(number: i32) -> bool {
|
||||
// Negative numbers can never be a palindrome because of the minus sign.
|
||||
if number < 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let number_string = number.to_string();
|
||||
number_string == number_string.chars().rev().collect::<String>()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_palindrome_number() {
|
||||
assert!(is_palindrome(0));
|
||||
assert!(is_palindrome(-0));
|
||||
assert!(is_palindrome(1));
|
||||
assert!(is_palindrome(121));
|
||||
assert!(is_palindrome(12321));
|
||||
|
||||
assert!(!is_palindrome(123));
|
||||
assert!(!is_palindrome(321));
|
||||
assert!(!is_palindrome(10));
|
||||
assert!(!is_palindrome(-121));
|
||||
}
|
Loading…
Reference in New Issue