Last active
October 17, 2019 19:05
-
-
Save smertelny/52f5a27a7fad0b3e76e5941d29fd6a65 to your computer and use it in GitHub Desktop.
My answer to Exercise from 8th part of RustBook Rust lang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::collections::HashMap; | |
| fn main() { | |
| let integer_list = [ | |
| 20, 47, 85, 99, 80, 86, 90, 89, 74, 9, 51, 78, 86, 36, 4, 68, 71, 42, 1, 64, | |
| ]; | |
| let mut result_vec: Vec<f64> = vec![0.0; 3]; | |
| result_vec[0] = integer_list.iter().sum::<i64>() as f64 / integer_list.iter().count() as f64; | |
| { | |
| let mut list_copy = integer_list.clone(); | |
| list_copy.sort(); | |
| result_vec[1] = list_copy[list_copy.iter().count() / 2] as f64; | |
| } | |
| { | |
| let mut count_map = HashMap::new(); | |
| for i in &integer_list { | |
| let count = count_map.entry(i).or_insert(0); | |
| *count += 1; | |
| } | |
| let mut max = [0, 0]; | |
| for (key, value) in &count_map { | |
| if *value > max[0] { | |
| max[0] = *value; | |
| max[1] = **key; | |
| } | |
| } | |
| result_vec[2] = max[1] as f64; | |
| } | |
| println!("{:?}", result_vec); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::collections::HashMap; | |
| use std::io::stdin; | |
| fn main() { | |
| println!("Hello! This is Employees terminal program"); | |
| println!("Enter names of employees and departments in format \"Add {{Employee-name}} to {{department-name}}\""); | |
| println!("To get list of all people - people-list; to list by departments - dep-list department-name"); | |
| println!("To exit - type \"exit\" or hit Ctrl + C\nGood luck!"); | |
| let mut user_input = String::new(); | |
| let mut employees: HashMap<String, Vec<String>> = HashMap::new(); | |
| loop { | |
| user_input.clear(); | |
| match stdin().read_line(&mut user_input) { | |
| Ok(_) => (), | |
| Err(error) => { | |
| println!("Failed to input: {}", error); | |
| continue; | |
| } | |
| } | |
| let user_input_array: Vec<&str> = user_input.trim().split(" ").collect(); | |
| if user_input_array.len() == 1 { | |
| match user_input_array[0].to_lowercase().as_ref() { | |
| "exit" => { | |
| println!("Bye! Exitting..."); | |
| break; | |
| } | |
| "people-list" => { | |
| let mut people: Vec<String> = Vec::new(); | |
| for names in employees.values() { | |
| for name in names { | |
| people.push(name.to_string()); | |
| } | |
| } | |
| people.sort(); | |
| println!("All persons in company: "); | |
| for person in &people { | |
| println!("{} ", person); | |
| } | |
| continue; | |
| } | |
| _ => { | |
| println!("There is no such command: {}", user_input_array[0]); | |
| continue; | |
| } | |
| } | |
| } | |
| if user_input_array.len() == 2 { | |
| if user_input_array[0].to_lowercase() == "dep-list" { | |
| if let Some(list) = employees.get(user_input_array[1]) { | |
| for person in list { | |
| println!("{}", person); | |
| } | |
| } else { | |
| println!("There is no department named {}", user_input_array[1]); | |
| } | |
| continue; | |
| } else { | |
| println!("There is no such command: {}", user_input_array[0]); | |
| continue; | |
| } | |
| } | |
| if user_input_array.len() == 4 { | |
| if user_input_array[0].to_lowercase() == "add" | |
| && user_input_array[2].to_lowercase() == "to" | |
| { | |
| let name = user_input_array[1].to_string(); | |
| let department = user_input_array[3].to_string(); | |
| employees.entry(department).or_insert(Vec::new()).push(name); | |
| } else { | |
| println!("Unknown command: {}", user_input_array.join(" ")); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment