From 0e5f84c3bf5a8547bebeb1d3eaf2c22d529bf7c4 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 18 Aug 2021 20:31:47 +0900 Subject: [PATCH] Fix rust_impl --- rust_impl/Cargo.lock | 22 ++++++++++++---------- rust_impl/Cargo.toml | 4 ++-- rust_impl/src/main.rs | 27 +++++++++++++++++++++------ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/rust_impl/Cargo.lock b/rust_impl/Cargo.lock index 13c5cb5..48cd792 100644 --- a/rust_impl/Cargo.lock +++ b/rust_impl/Cargo.lock @@ -1,11 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "RunLengthEncoding" -version = "0.1.0" -dependencies = [ - "structopt", -] +version = 3 [[package]] name = "ansi_term" @@ -120,6 +115,13 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "run_length_encoding" +version = "0.1.0" +dependencies = [ + "structopt", +] + [[package]] name = "strsim" version = "0.8.0" @@ -128,9 +130,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "structopt" -version = "0.3.21" +version = "0.3.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5277acd7ee46e63e5168a80734c9f6ee81b1367a7d8772a2d765df2a3705d28c" +checksum = "69b041cdcb67226aca307e6e7be44c8806423d83e018bd662360a93dabce4d71" dependencies = [ "clap", "lazy_static", @@ -139,9 +141,9 @@ dependencies = [ [[package]] name = "structopt-derive" -version = "0.4.14" +version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ba9cdfda491b814720b6b06e0cac513d922fc407582032e8706e9f137976f90" +checksum = "7813934aecf5f51a54775e00068c237de98489463968231a51746bbbc03f9c10" dependencies = [ "heck", "proc-macro-error", diff --git a/rust_impl/Cargo.toml b/rust_impl/Cargo.toml index c920d50..a7cb92c 100644 --- a/rust_impl/Cargo.toml +++ b/rust_impl/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "RunLengthEncoding" +name = "run_length_encoding" version = "0.1.0" authors = ["Stephen Seo "] edition = "2018" @@ -7,4 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -structopt = "0.3.21" +structopt = "0.3.22" diff --git a/rust_impl/src/main.rs b/rust_impl/src/main.rs index fc7d9b3..55e2918 100644 --- a/rust_impl/src/main.rs +++ b/rust_impl/src/main.rs @@ -1,5 +1,4 @@ use structopt::StructOpt; -use std::collections::HashMap; #[derive(StructOpt, Debug)] #[structopt(name = "RunLengthEncoding")] @@ -10,13 +9,29 @@ struct Opts { fn main() { let opts = Opts::from_args(); - let mut map: HashMap = HashMap::new(); - + let mut current: Option = None; + let mut current_count: u32 = 0; + let mut list: Vec<(char, u32)> = Vec::new(); for c in opts.input.chars() { - map.get_mut(&c).map(|v| *v += 1).or_else(|| {map.insert(c, 1); Some(())}); + if c.is_ascii_whitespace() { + continue; + } else { + if current.is_none() || current.unwrap() != c { + if current_count > 0 { + list.push((current.unwrap(), current_count)); + } + current_count = 1; + current = Some(c); + } else { + current_count += 1; + } + } + } + if current_count > 0 && current.is_some() { + list.push((current.unwrap(), current_count)); } - for (c, value) in map { - println!("{} with value {}", c, value); + for (c, count) in list { + println!("{}: has count of {}", c, count); } }