Sha256: 2f5f4690d41acc6eaa27c4c7fb7aa4fe779f67a5dfadf1ff4259086c9af0b741
Contents?: true
Size: 1.35 KB
Versions: 237
Compression:
Stored size: 1.35 KB
Contents
use std::cmp; pub fn encode(input: &str) -> String { input .chars() .fold((String::new(), ' ', 0, 1), |(mut acc, last, last_n, pos), c| { // acc = where answer is accumulated // last = last character read // last_n = accum count for last if c == last { if pos == input.len() { // end of string acc += (last_n + 1).to_string().as_str(); acc.push(c); } (acc, last, last_n + 1, pos + 1) } else { if last_n > 1 { acc += last_n.to_string().as_str(); } if last_n > 0 { // ignore initial last (single whitespace) acc.push(last); } if pos == input.len() { // end of string acc.push(c); } (acc, c, 1, pos + 1) } }).0 } pub fn decode(input: &str) -> String { input .chars() .fold((String::new(), 0), |(mut acc, last_n), c| { if let Some(d) = c.to_digit(10) { (acc, 10*last_n + d) } else { acc += c.to_string() .repeat(cmp::max(last_n, 1) as usize).as_str(); (acc, 0) } }).0 }
Version data entries
237 entries across 237 versions & 1 rubygems