From c8390c584fa3e5d3c1d8fd7b0aec86d66cfa1d5e Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 6 Apr 2022 14:31:30 +0200 Subject: [PATCH] Solve palindrome-number. --- source/lib.rs | 1 + source/palindrome_number/mod.rs | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 source/palindrome_number/mod.rs diff --git a/source/lib.rs b/source/lib.rs index 9823fe9..1b37410 100644 --- a/source/lib.rs +++ b/source/lib.rs @@ -1,2 +1,3 @@ +pub mod palindrome_number; pub mod reverse_integer; pub mod two_sum; diff --git a/source/palindrome_number/mod.rs b/source/palindrome_number/mod.rs new file mode 100644 index 0000000..799d715 --- /dev/null +++ b/source/palindrome_number/mod.rs @@ -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::() +} + +#[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)); +}