Created
May 15, 2023 16:26
-
-
Save keturiosakys/b90ec0092b3c063f7316687c13947ed0 to your computer and use it in GitHub Desktop.
Binary palindrome checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let num = 5; | |
let res = binary_pal(num); | |
println!("{}", res); | |
} | |
fn binary_pal(num: i32) -> bool { | |
let binary = format!("{:b}", num); | |
let reversed: String = binary.chars().rev().collect(); | |
if binary == reversed { | |
true | |
} else { | |
false | |
} | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn test_binary_pal() { | |
assert_eq!(true, binary_pal(5)); | |
assert_eq!(false, binary_pal(10)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment