diff --git a/collections/Cargo.toml b/collections/Cargo.toml new file mode 100644 index 0000000..ecb491a --- /dev/null +++ b/collections/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "collections" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/collections/src/main.rs b/collections/src/main.rs new file mode 100644 index 0000000..f03d236 --- /dev/null +++ b/collections/src/main.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; + +fn main() { + let mut v = vec![1, 2, 3, 4, 5]; + + let first = &v[0]; + + println!("The first element is: {}", first); + v.push(6); + let s1 = String::from("Hello, "); + let s2 = String::from("world!"); + let s3 = s1 + &s2; + + println!("{}", s3); + + let s1 = String::from("tic"); + let s2 = String::from("tac"); + let s3 = String::from("toe"); + + let s = s1 + "-" + &s2 + "-" + &s3; + + println!("{}", s); + let s1 = String::from("tic"); + let s2 = String::from("tac"); + let s3 = String::from("toe"); + + let s = format!("{}-{}-{}", s1, s2, s3); + println!("{}", s); + for c in "नमस्ते".chars() { + println!("{}", c); + } + + let mut scores = HashMap::new(); + + scores.insert(String::from("Blue"), 10); + + scores.entry(String::from("Blue")).or_insert(50); + scores.entry(String::from("Yellow")).or_insert(50); + + let team_name = String::from("Blue"); + let score = scores.get(&team_name); + if let Some(i) = score { + println!("{} team score is {}", team_name, i); + } else { + println!("{} team not found", team_name); + } + + for (key, value) in &scores { + println!("{}: {}", key, value); + } + + + let text = "hello world wonderful world"; + + let mut map = HashMap::new(); + + for word in text.split_whitespace() { + let count = map.entry(word).or_insert(0); + *count += 1; + } + + println!("{:?}", map); +} diff --git a/numbers/Cargo.toml b/numbers/Cargo.toml new file mode 100644 index 0000000..ee7bf14 --- /dev/null +++ b/numbers/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "numbers" +version = "0.1.0" +authors = ["gbrochar "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/numbers/src/main.rs b/numbers/src/main.rs new file mode 100644 index 0000000..5443370 --- /dev/null +++ b/numbers/src/main.rs @@ -0,0 +1,73 @@ +use std::collections::HashMap; + +fn get_mean(input: &Vec) -> f64 { + let mut total = 0; + for n in input { + total += n; + } + total as f64 / input.len() as f64 +} + +fn get_median(input: &Vec) -> f64 { + let mut tmp = input.clone(); + let mut i: usize = 0; + while i < tmp.len() - 1 { + if &tmp[i] > &tmp[i + 1] { + tmp.swap(i, i + 1); + } else { + i += 1; + } + } + if tmp.len() % 2 == 1 { + tmp[tmp.len() / 2] as f64 + } else { + (tmp[tmp.len() / 2 - 1] + tmp[tmp.len() / 2]) as f64 / 2.0 + } +} + +fn get_mode(input: &Vec) -> Option { + let mut map = HashMap::new(); + let mut max: (Option, Option) = (None, None); + let mut none = true; + + for &n in input { + let count = map.entry(n).or_insert(0); + *count += 1; + } + + for (&key, &value) in &map { + if Some(value) > max.1 { + max = (Some(key), Some(value)); + none = false; + } else if Some(value) == max.1 { + none = true; + } + } + + if none { + None + } else { + max.0 + } +} + +fn main() { + let input1: Vec = vec![-23, 46, 10, 45, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 23, 46, 23, 23, 10, 12, 94]; + let input2: Vec = vec![1, 2, 3, 4]; + + println!("Vec input1 : {:?}", input1); + println!("Average of input1 is {}", get_mean(&input1)); + println!("Median of input1 is {}", get_median(&input1)); + match get_mode(&input1) { + Some(v) => println!("Mode of input1 is {:?}", v), + None => println!("input1 has no mode.") + } + + println!("Vec input2 : {:?}", input2); + println!("Average of input2 is {}", get_mean(&input2)); + println!("Median of input2 is {}", get_median(&input2)); + match get_mode(&input2) { + Some(v) => println!("Mode of input2 is {:?}", v), + None => println!("input2 has no mode.") + } +}