collections

This commit is contained in:
gbrochar 2020-12-05 16:21:20 +01:00
parent 908aefc86c
commit 4dcce6cfc2
4 changed files with 154 additions and 0 deletions

9
collections/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "collections"
version = "0.1.0"
authors = ["gbrochar <gbrochar@student.42.fr>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

63
collections/src/main.rs Normal file
View File

@ -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);
}

9
numbers/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "numbers"
version = "0.1.0"
authors = ["gbrochar <gbrochar@student.42.fr>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

73
numbers/src/main.rs Normal file
View File

@ -0,0 +1,73 @@
use std::collections::HashMap;
fn get_mean(input: &Vec<i32>) -> f64 {
let mut total = 0;
for n in input {
total += n;
}
total as f64 / input.len() as f64
}
fn get_median(input: &Vec<i32>) -> 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<i32>) -> Option<i32> {
let mut map = HashMap::new();
let mut max: (Option<i32>, Option<i32>) = (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<i32> = 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<i32> = 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.")
}
}