Created
April 9, 2019 21:35
-
-
Save rust-play/ccc48158f5ba8ac190526eb00489e841 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or 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
extern crate itertools; // 0.8.0 | |
use itertools::Itertools; // 0.8.0 | |
fn g(x: u32) -> u32 { | |
let s = x.to_string(); | |
s.chars() | |
.map(|c| c.to_digit(10).unwrap()) | |
.into_iter() | |
.group_by(|&x| x) | |
.into_iter() | |
.map(|(k, group)| (group.count() as u32, k)) | |
.fold(0, |acc, (repeats, digit)| acc * 100 + repeats * 10 + digit) | |
} | |
fn main() { | |
println!("{:?} should be 221311", g(2231)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment