Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created April 9, 2019 21:35
Show Gist options
  • Save rust-play/ccc48158f5ba8ac190526eb00489e841 to your computer and use it in GitHub Desktop.
Save rust-play/ccc48158f5ba8ac190526eb00489e841 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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