From 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb Mon Sep 17 00:00:00 2001 From: Orangerot Date: Thu, 27 Jun 2024 11:30:16 +0200 Subject: Initial commit --- palindrome-number/src/main.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 palindrome-number/src/main.rs (limited to 'palindrome-number/src') diff --git a/palindrome-number/src/main.rs b/palindrome-number/src/main.rs new file mode 100644 index 0000000..1e0d6d1 --- /dev/null +++ b/palindrome-number/src/main.rs @@ -0,0 +1,31 @@ +fn main() { + println!("Hello, world!"); + let tests = [ + (121, true), + (-121, false), + (10, false), + (1410110141, true) + ]; + for test in tests { + println!("{} is {} should be {}", test.0, Solution::is_palindrome(test.0), test.1); + } +} + + +struct Solution {} +impl Solution { + pub fn is_palindrome(x: i32) -> bool { + if x < 0 {return false;} + let len = (x as f32).log10() as usize; + //println!("{} {}", len, (len+1)/2); + for i in 0..(len+1)/2 { + if (x as u32 / 10_u32.pow(i as u32)) % 10 != (x as u32 / 10_u32.pow((len - i) as u32)) % 10 { + return false; + } + } + true + } + pub fn get(x: i32, i: usize) -> u32 { + (x as u32 / 10_u32.pow(i as u32)) % 10 + } +} -- cgit v1.2.3