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

Version Path
trackler-2.2.1.179 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.178 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.177 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.176 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.175 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.174 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.173 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.172 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.171 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.170 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.169 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.167 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.166 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.165 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.164 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.163 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.162 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.161 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.160 tracks/rust/exercises/run-length-encoding/example.rs
trackler-2.2.1.159 tracks/rust/exercises/run-length-encoding/example.rs