Not sure if this thread is still active, but I found it while doing the exercises in chapter 8 and wanted to compare my solutions to how other people have solved them, just like the thread starter.
Feedback is appreciated!
calculate median, mode and average:
use std::collections::HashMap;
fn main() {
let numbers = vec![10, 12, 24, 36, 42, 35, 46, 60, 19, 10, 12];
let avg = calculate_average(&numbers);
let median = calculate_median(&numbers);
let mode = calculate_mode(&numbers);
println!("median: {}", median);
println!("average: {}", avg);
println!("mode: {}", mode);
}
fn calculate_median(numbers: &Vec<i32>) -> f32 {
let mut sorted = numbers.clone();
sorted.sort();
let mid = sorted.len() / 2;
if sorted.len() % 2 == 0 {
let x = sorted[mid] as f32;
let y = sorted[mid - 1] as f32;
return (x + y) * 0.5;
}
sorted[mid] as f32
}
fn calculate_mode(numbers: &Vec<i32>) -> i32 {
let mut times = HashMap::new();
for x in numbers {
let count = times.entry(x).or_insert(0);
*count += 1;
}
let mut best_key = numbers[0];
let mut best_val = times.get(&best_key).unwrap();
for (key, value) in × {
if value > best_val {
best_key = **key;
best_val = value;
}
}
best_key
}
fn calculate_average(numbers: &Vec<i32>) -> f32 {
let mut sum = 0;
for x in numbers {
sum += x;
}
sum as f32 / numbers.len() as f32
// numbers.iter().sum::<i32>() as f32 / numbers.len() as f32
}
piglatin
use std::collections::HashMap;
fn main() {
let t1 = String::from("first");
let p1 = to_piglatin(&t1);
assert_eq!(String::from("irst-fay"), p1);
let t2 = String::from("apple");
let p2 = to_piglatin(&t2);
assert_eq!(String::from("apple-hay"), p2);
let t3 = String::from("first apple");
let p3 = to_piglatin(&t3);
assert_eq!(String::from("irst-fay apple-hay"), p3);
}
fn to_piglatin(text: &String) -> String {
let vowels = vec!['a', 'o', 'u', 'e', 'i', 'y'];
let words = text.split_whitespace();
let mut output = String::new();
for (i, word) in words.enumerate() {
let first_char = word.chars().next().unwrap();
if vowels.contains(&first_char) {
let pig_word = format!("{}-{}", word, "hay");
if i == 0 {
output = pig_word;
} else {
output = format!("{} {}", output, pig_word);
}
} else {
let pig_word = format!("{}-{}", &word[1..], format!("{}{}", &word[..1], "ay"));
if i == 0 {
output = pig_word;
} else {
output = format!("{} {}", output, pig_word);
}
}
}
output
}
Employee list
use std::collections::HashMap;
fn main() {
{
let mut company: HashMap<String, Vec<String>> = HashMap::new();
// handle_command(&command, &company);
let (name, dep) = get_name_and_department("Add Sally to Engineering".to_string());
company.entry(dep).or_insert(Vec::new()).push(name);
let (name, dep) = get_name_and_department("Add Amir to Sales".to_string());
company.entry(dep).or_insert(Vec::new()).push(name);
let (name, dep) = get_name_and_department("Add Beate to Sales".to_string());
company.entry(dep).or_insert(Vec::new()).push(name);
let (name, dep) = get_name_and_department("Add Cecil to Marketing".to_string());
company.entry(dep).or_insert(Vec::new()).push(name);
let sorted = get_all_employees(&company);
println!("SORTED: {:?}", sorted);
println!("{:?}", company);
}
}
fn get_all_employees(company: &HashMap<String, Vec<String>>) -> Vec<String> {
let mut employees: Vec<String> = Vec::new();
for (_, value) in company {
employees = [employees, value.clone()].concat();
}
employees.sort();
employees
}
fn get_name_and_department(s: String) -> (String, String) {
let words = text_to_words(&s);
return (String::from(words[1]), String::from(words[words.len() - 1]));
}
fn text_to_words(s: &String) -> Vec<&str> {
let bytes = s.as_bytes();
let num_bytes = bytes.len();
let mut words = Vec::new();
let mut last_whitespace = 0;
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
words.push(&s[last_whitespace..i]);
last_whitespace = i + 1;
} else if i == num_bytes - 1 {
words.push(&s[last_whitespace..]);
}
}
return words;
}
Wanted to do a fn that takes the company hashmap and adds the employee to the deparment based on the command provided, but did not manage due to some ownership issues...
This is what the handle_command fn looked like
fn main () {
let mut company: HashMap<String, Vec<String>> = HashMap::new();
handle_command("Add Sally to Engineering".to_string(), &company);
println!("{:?}", company);
}
fn handle_command(command: String, company: &mut HashMap<String, Vec<String>>) {
let (name, dep) = get_name_and_department(command);
company.entry(dep).or_insert(Vec::new()).push(name);
}
and the error:
mismatched types
types differ in mutability
note: expected mutable reference `&mut std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>`
found reference `&std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>`rustc(E0308)
main.rs(145, 64): types differ in mutability