This commit is contained in:
Jasper Ras 2026-01-15 09:09:24 +01:00
parent eb9773627e
commit 10b29ad9cb
14 changed files with 164 additions and 0 deletions

BIN
ch8/ch10/hash Executable file

Binary file not shown.

12
ch8/ch10/hash.rs Normal file
View file

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

BIN
ch8/ch10/median_int Executable file

Binary file not shown.

32
ch8/ch10/median_int.rs Normal file
View file

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

BIN
ch8/ch10/or_insert Executable file

Binary file not shown.

11
ch8/ch10/or_insert.rs Normal file
View file

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

BIN
ch8/ch10/overwrite Executable file

Binary file not shown.

11
ch8/ch10/overwrite.rs Normal file
View file

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

BIN
ch8/ch10/pig_latin Executable file

Binary file not shown.

22
ch8/ch10/pig_latin.rs Normal file
View file

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

BIN
ch8/ch10/update_old Executable file

Binary file not shown.

13
ch8/ch10/update_old.rs Normal file
View file

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

BIN
ch8/ch10/usrmgt Executable file

Binary file not shown.

63
ch8/ch10/usrmgt.rs Normal file
View file

@ -0,0 +1,63 @@
use std::io;
use std::collections::HashMap;
fn main() {
let mut departments: HashMap<String, Vec<String>> = 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<usize> = 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!("");
}
}