fn flip_the_number(x: &u64) -> u64 { let mut x = *x; let mut y = 0; while x != 0 { y = y * 10 + x % 10; x /= 10; } y }
- fn flip_the_number(x: &u64) -> u64 {
x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap()- let mut x = *x;
- let mut y = 0;
- while x != 0 {
- y = y * 10 + x % 10;
- x /= 10;
- }
- y
- }
#[test] fn test() { let nums = [ (1234, 4321), (1234567890, 987654321), (243, 342), (12, 21), (837583, 385738), (32851532, 23515823), (999999, 999999), (111, 111), (35832456789, 98765423853), (9223372036854775807, 7085774586302733229) ]; for num in nums.iter() { assert_eq!(num.1, flip_the_number(&num.0)); } }
- #[test]
- fn test() {
for num in NUMS.iter() {- let nums = [
- (1234, 4321),
- (1234567890, 987654321),
- (243, 342),
- (12, 21),
- (837583, 385738),
- (32851532, 23515823),
- (999999, 999999),
- (111, 111),
- (35832456789, 98765423853),
- (9223372036854775807, 7085774586302733229)
- ];
- for num in nums.iter() {
- assert_eq!(num.1, flip_the_number(&num.0));
- }
- }
Numbers
Data Types
Integers
Algorithms
Logic
use std::collections::BTreeMap; fn digits(n: u64) -> usize { let powers = (0..20).map(|n| ( if n == 0 { 0 } else { 10_u64.pow(n) }, n as usize + 1 )).collect::<BTreeMap<_, _>>(); return *powers.range(..=n).last().unwrap().1; }
- use std::collections::BTreeMap;
use std::ops::Bound::Unbounded;use std::ops::Bound::Included;- fn digits(n: u64) -> usize {
let mut powers = BTreeMap::new();powers.insert(0, 1);powers.insert(10, 2);powers.insert(100, 3);powers.insert(1000, 4);powers.insert(10000, 5);powers.insert(100000, 6);powers.insert(1000000, 7);powers.insert(10000000, 8);powers.insert(100000000, 9);powers.insert(1000000000, 10);powers.insert(10000000000, 11);powers.insert(100000000000, 12);powers.insert(1000000000000, 13);powers.insert(10000000000000, 14);powers.insert(100000000000000, 15);powers.insert(1000000000000000, 16);powers.insert(10000000000000000, 17);powers.insert(100000000000000000, 18);powers.insert(1000000000000000000, 19);powers.insert(10000000000000000000, 20);return *powers.range((Unbounded, Included(n))).last().unwrap().1;- let powers = (0..20).map(|n| (
- if n == 0 { 0 } else { 10_u64.pow(n) },
- n as usize + 1
- )).collect::<BTreeMap<_, _>>();
- return *powers.range(..=n).last().unwrap().1;
- }
Games
Arrays
Data Types
Algorithms
Logic
fn required_energy(heights: Vec<i64>) -> i64 { heights .windows(2) .map(|y| (y[1] - y[0]).abs()) .sum::<i64>() }
- fn required_energy(heights: Vec<i64>) -> i64 {
- heights
.iter().skip(1).zip(heights.iter()).map(|(y2, y1)| (y2 - y1).abs())- .windows(2)
- .map(|y| (y[1] - y[0]).abs())
- .sum::<i64>()
- }