fn flip_the_number(x: &u64) -> u64 { x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap() }
def flip_the_number(x):return int(str(x)[::-1])- fn flip_the_number(x: &u64) -> u64 {
- x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap()
- }
#[test] fn test() { for num in NUMS.iter() { assert_eq!(num.1, flip_the_number(&num.0)); } }
from random import randinttest.describe("Basic Tests")test.assert_equals(flip_the_number("12345"), 54321)test.assert_equals(flip_the_number("1973572"), 2753791)test.assert_equals(flip_the_number(1973572), 2753791)test.describe("Random tests")for x in range(100):num = randint(0, 1000000000)if x % 2 == 0:test.assert_equals(flip_the_number(num), int(str(num)[::-1]))else:test.assert_equals(flip_the_number(str(num)), int(str(num)[::-1]))- #[test]
- fn test() {
- for num in NUMS.iter() {
- assert_eq!(num.1, flip_the_number(&num.0));
- }
- }
Numbers
Data Types
Integers
Algorithms
Logic
fn digits(n: u64) -> usize { match n { 0 => 1, _ => (n as f64).log10().floor() as usize + 1 } }
fn digits(n: u64, base: u64) -> usize {digits_iter(n, base).count()}fn digits_iter(n: u64, base: u64) -> impl Iterator<Item = u64> {let mut n = Some(n);std::iter::from_fn(move || match n {Some(x) => {let digit = x % base;n = Some(x / base);if let Some(0) = n {n = None;}Some(digit)}_ => None,})}- fn digits(n: u64) -> usize {
- match n {
- 0 => 1,
- _ => (n as f64).log10().floor() as usize + 1
- }
- }
#[test] fn pow10() { assert_eq!(digits(0), 1); for i in 0..20 { assert_eq!(digits(POW10[i]), i+1); } assert_eq!(digits(std::u64::MAX), 20); } #[test] fn pow10_minus_10_perc() { for i in 1..20 { assert_eq!(digits(POW10[i] - (POW10[i]/10) as u64), i); } } #[test] fn pow10_half() { for i in 1..20 { assert_eq!(digits(POW10[i] / 2), i); } }
- #[test]
- fn pow10() {
assert_eq!(digits(0, 10), 1);- assert_eq!(digits(0), 1);
- for i in 0..20 {
assert_eq!(digits(POW10[i], 10), i+1);- assert_eq!(digits(POW10[i]), i+1);
- }
assert_eq!(digits(std::u64::MAX, 10), 20);- assert_eq!(digits(std::u64::MAX), 20);
- }
- #[test]
fn pow10_minus_1() {- fn pow10_minus_10_perc() {
- for i in 1..20 {
assert_eq!(digits(POW10[i] - 1, 10), i);- assert_eq!(digits(POW10[i] - (POW10[i]/10) as u64), i);
- }
- }
- #[test]
- fn pow10_half() {
- for i in 1..20 {
assert_eq!(digits(POW10[i] / 2, 10), i);- assert_eq!(digits(POW10[i] / 2), i);
- }
}#[test]fn base2(){for i in 1..20{assert_eq!(digits(2u64.pow(i), 2), (i+1) as usize);}}#[test]fn base16(){for i in 1..10{assert_eq!(digits(16u64.pow(i), 16), (i+1) as usize);}- }