Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Created May 15, 2023 16:26
Show Gist options
  • Save keturiosakys/b90ec0092b3c063f7316687c13947ed0 to your computer and use it in GitHub Desktop.
Save keturiosakys/b90ec0092b3c063f7316687c13947ed0 to your computer and use it in GitHub Desktop.
Binary palindrome checker
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