diff --git a/ch8/ch10/hash b/ch8/ch10/hash new file mode 100755 index 0000000..7d42b90 Binary files /dev/null and b/ch8/ch10/hash differ diff --git a/ch8/ch10/hash.rs b/ch8/ch10/hash.rs new file mode 100644 index 0000000..20883c1 --- /dev/null +++ b/ch8/ch10/hash.rs @@ -0,0 +1,12 @@ +use std::collections::HashMap; + +fn main() { + let field_name = String::from("field"); + let field_value = String::from("value"); + + let mut map = HashMap::new(); + map.insert(field_name, &field_value); + + print!("{}", map.get("field").unwrap()); + print!("{}", field_value); +} diff --git a/ch8/ch10/median_int b/ch8/ch10/median_int new file mode 100755 index 0000000..9ab37b6 Binary files /dev/null and b/ch8/ch10/median_int differ diff --git a/ch8/ch10/median_int.rs b/ch8/ch10/median_int.rs new file mode 100644 index 0000000..a88b5e6 --- /dev/null +++ b/ch8/ch10/median_int.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +fn main() { + let mut numbers = vec![100, 23, 1, 2, 100, 38, 57, 100, 23, 44, 163]; + numbers.sort(); + + let is_even = numbers.len() % 2 == 0; + let median: i32; + let mut count = HashMap::new(); + + if is_even { + let n1 = numbers[(numbers.len() / 2) - 1]; + let n2 = numbers[numbers.len() / 2]; + median = (n1 + n2) / 2; + } else { + median = numbers[numbers.len() / 2]; + } + + let mut mode: i32 = 0; + + for num in numbers { + let entry = count.entry(num).or_insert(0); + *entry += 1; + + if *entry > count.get(&mode).copied().unwrap_or(0) { + mode = num; + } + } + + println!("median: {median}"); + println!("mode: {mode}"); +} diff --git a/ch8/ch10/or_insert b/ch8/ch10/or_insert new file mode 100755 index 0000000..2f49d85 Binary files /dev/null and b/ch8/ch10/or_insert differ diff --git a/ch8/ch10/or_insert.rs b/ch8/ch10/or_insert.rs new file mode 100644 index 0000000..628b1da --- /dev/null +++ b/ch8/ch10/or_insert.rs @@ -0,0 +1,11 @@ +use std::collections::HashMap; + +fn main() { + let mut scores = HashMap::new(); + scores.insert(String::from("Blue"), 10); + + scores.entry(String::from("Yellow")).or_insert(50); + scores.entry(String::from("Blue")).or_insert(50); + + println!("{scores:?}"); +} diff --git a/ch8/ch10/overwrite b/ch8/ch10/overwrite new file mode 100755 index 0000000..473b577 Binary files /dev/null and b/ch8/ch10/overwrite differ diff --git a/ch8/ch10/overwrite.rs b/ch8/ch10/overwrite.rs new file mode 100644 index 0000000..2b7d344 --- /dev/null +++ b/ch8/ch10/overwrite.rs @@ -0,0 +1,11 @@ +use std::collections::HashMap; + +fn main() { + let mut scores = HashMap::new(); + + scores.insert(String::from("Blue"), 10); + scores.insert(String::from("Blue"), 25); + + println!("{scores:?}"); +} + diff --git a/ch8/ch10/pig_latin b/ch8/ch10/pig_latin new file mode 100755 index 0000000..7edf6ad Binary files /dev/null and b/ch8/ch10/pig_latin differ diff --git a/ch8/ch10/pig_latin.rs b/ch8/ch10/pig_latin.rs new file mode 100644 index 0000000..8e6a691 --- /dev/null +++ b/ch8/ch10/pig_latin.rs @@ -0,0 +1,22 @@ +fn main() { + let vowels = vec!['a', 'e', 'i', 'o', 'u']; + let words = vec!["flow", "fool", "dick", "cock", "sex", "ape", "eel", "apple", "addicted", "ore"]; + + for word in words { + let c = word.chars().next().unwrap(); + + let mut adj = format!("-{c}ay"); + + for vowel in vowels.iter() { + if c != *vowel { + continue; + } + + adj = String::from("-hay"); + break; + } + + let w = String::from(word) + &adj; + println!("{w}"); + } +} diff --git a/ch8/ch10/update_old b/ch8/ch10/update_old new file mode 100755 index 0000000..44502e5 Binary files /dev/null and b/ch8/ch10/update_old differ diff --git a/ch8/ch10/update_old.rs b/ch8/ch10/update_old.rs new file mode 100644 index 0000000..3f5a3df --- /dev/null +++ b/ch8/ch10/update_old.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +fn main() { + 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/ch8/ch10/usrmgt b/ch8/ch10/usrmgt new file mode 100755 index 0000000..a27e12b Binary files /dev/null and b/ch8/ch10/usrmgt differ diff --git a/ch8/ch10/usrmgt.rs b/ch8/ch10/usrmgt.rs new file mode 100644 index 0000000..ea09da5 --- /dev/null +++ b/ch8/ch10/usrmgt.rs @@ -0,0 +1,63 @@ +use std::io; +use std::collections::HashMap; + +fn main() { + let mut departments: HashMap> = HashMap::new(); + + loop { + let mut input = String::new(); + io::stdin().read_line(&mut input).unwrap(); + + let words: Vec<&str> = input.split_whitespace().collect(); + + if words.is_empty() { continue; } + + match words[0].to_lowercase().as_str() { + "add" => { + // Format: add [name] [department] + if words.len() >= 3 { + let v = departments.entry(String::from(words[2])).or_insert(Vec::new()); + v.push(String::from(words[1])); + } + }, + "del" => { + // Format: del [name] [department] + if words.len() >= 3 { + let v = departments.entry(String::from(words[2])).or_insert(Vec::new()); + let mut idx: Option = None; + + for (i, who2) in v.into_iter().enumerate() { + if who2 == words[1] { + idx = Some(i); + break; + } + } + + match idx { + Some(x) => { v.remove(x); () }, + None => (), + }; + } + }, + "ls" => { + if words.len() == 1 { + println!("{departments:?}"); + } + if words.len() >= 2 { + let dpt = String::from(words[1]); + let ppl = departments.entry(dpt.clone()).or_insert(Vec::new()); + ppl.sort(); + + println!("The following people are in the {} department:", dpt); + + for pp in ppl { + println!("{pp}"); + } + } + }, + _ => println!("Unknown command"), + } + + println!(""); + } +}