diff options
author | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-06-27 11:30:16 +0200 |
commit | 4b0a6a01b051a4ebfbc17661d14cb23fe4f275fb (patch) | |
tree | 0072cca328fe5adb2ed61004010228ff85e2164d /palindrome-number/src |
Diffstat (limited to 'palindrome-number/src')
-rw-r--r-- | palindrome-number/src/main.rs | 31 |
1 files changed, 31 insertions, 0 deletions
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 + } +} |