Solve collection tasks
This commit is contained in:
parent
0bae9c88d5
commit
ed3b93131f
3 changed files with 315 additions and 0 deletions
140
collections/Cargo.lock
generated
Normal file
140
collections/Cargo.lock
generated
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg-if"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "collections"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"rand",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libc"
|
||||||
|
version = "0.2.169"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro2"
|
||||||
|
version = "1.0.93"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quote"
|
||||||
|
version = "1.0.38"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "2.0.96"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicode-ident"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "11cd88e12b17c6494200a9c1b683a04fcac9573ed74cd1b62aeb2727c5592243"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||||
|
dependencies = [
|
||||||
|
"byteorder",
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
7
collections/Cargo.toml
Normal file
7
collections/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "collections"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
168
collections/src/main.rs
Normal file
168
collections/src/main.rs
Normal file
|
@ -0,0 +1,168 @@
|
||||||
|
use std::{collections::HashMap, io::{self, Write}};
|
||||||
|
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// Task 1: Statistics
|
||||||
|
let median: f32;
|
||||||
|
let mode;
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let mut values = Vec::new();
|
||||||
|
|
||||||
|
println!("values:");
|
||||||
|
|
||||||
|
for _ in 1..=rng.gen_range(1..10) {
|
||||||
|
let value = rng.gen_range(1..5);
|
||||||
|
println!("{value}");
|
||||||
|
values.push(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
median = if values.len() == 1 {
|
||||||
|
values[0] as f32
|
||||||
|
} else {
|
||||||
|
match values.len() % 2 {
|
||||||
|
0 => ((values[values.len() / 2] + values[(values.len() / 2) - 1]) as f32) / 2.0,
|
||||||
|
_ => values[values.len() / 2] as f32
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut occurrences = HashMap::new();
|
||||||
|
|
||||||
|
for number in &values {
|
||||||
|
*occurrences.entry(*number).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
mode = *occurrences.iter().max_by_key(|e| e.1).unwrap().0;
|
||||||
|
println!("number of elements: {}", values.len());
|
||||||
|
println!("median: {median}");
|
||||||
|
println!("mode: {mode}");
|
||||||
|
|
||||||
|
// Task 2:
|
||||||
|
let words = ["first", "apple"];
|
||||||
|
|
||||||
|
for word in words {
|
||||||
|
let mut result;
|
||||||
|
|
||||||
|
if ['a','e', 'i', 'o', 'u', 'ä', 'ö', 'ü'].contains(&word.to_lowercase().chars().next().unwrap()) {
|
||||||
|
result = String::from(word);
|
||||||
|
result.push_str("-h");
|
||||||
|
} else {
|
||||||
|
result = String::from(&word[1..]);
|
||||||
|
result.push('-');
|
||||||
|
result.push_str(&word[0..=0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push_str("ay");
|
||||||
|
println!("input: {word}");
|
||||||
|
println!("pig latin: {result}");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!();
|
||||||
|
|
||||||
|
// Task 3:
|
||||||
|
let mut exit = false;
|
||||||
|
let mut employees: HashMap<String, Vec<String>> = HashMap::new();
|
||||||
|
println!("Welcome to the human resources management console!");
|
||||||
|
println!("Please enter a command or 'help' for the help message.");
|
||||||
|
|
||||||
|
while !exit {
|
||||||
|
let mut input = String::new();
|
||||||
|
print!("> ");
|
||||||
|
io::stdout().flush().expect("An error occurred");
|
||||||
|
io::stdin().read_line(&mut input).expect("An error occurred");
|
||||||
|
|
||||||
|
match input.to_lowercase().trim() {
|
||||||
|
"q" | "quit" => exit = true,
|
||||||
|
"h" | "help" => {
|
||||||
|
println!("This program allows you to manage employees and their corresponding department.");
|
||||||
|
println!();
|
||||||
|
println!("Following commands are available:");
|
||||||
|
println!("p, print [department]: prints all employees or the employees of the specified department");
|
||||||
|
println!();
|
||||||
|
println!("a, add {{employee}} to {{department}}: adds the specified employee to the specified department");
|
||||||
|
println!();
|
||||||
|
println!("r, remove {{employee}} from {{department}}: adds the specified employee to the specified department");
|
||||||
|
println!();
|
||||||
|
println!("q, quit: closes the program");
|
||||||
|
println!();
|
||||||
|
println!("h, help: prints this help message");
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let arguments = input.split_whitespace();
|
||||||
|
|
||||||
|
if arguments.clone().count() > 0 {
|
||||||
|
let command = arguments.clone().next().expect("An error occurred");
|
||||||
|
|
||||||
|
match command.to_lowercase().as_str() {
|
||||||
|
"a" | "add" => {
|
||||||
|
match input[command.len()..].split(" to ").collect::<Vec<&str>>().as_slice() {
|
||||||
|
[employee, department] => {
|
||||||
|
employees.entry(department.trim().to_string()).or_insert(Vec::new()).push(employee.trim().to_string());
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
println!("The syntax of the command \"{input}\" is incorrect!");
|
||||||
|
println!("Please try again");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"r" | "remove" => {
|
||||||
|
match input[command.len()..].split(" from ").collect::<Vec<&str>>().as_slice() {
|
||||||
|
[employee, department] => {
|
||||||
|
employees.entry(department.trim().to_string()).and_modify(|e| e.retain(|e| *e != employee.trim()));
|
||||||
|
employees.retain(|_, e| !e.is_empty());
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
println!("The syntax of the command \"{input}\" is incorrect!");
|
||||||
|
println!("Please try again");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"p" | "print" => {
|
||||||
|
if input.trim().len() > command.len() {
|
||||||
|
let department = input[command.len()..].trim();
|
||||||
|
|
||||||
|
match employees.get(department) {
|
||||||
|
Some(employees) => {
|
||||||
|
println!("Employees in {department}:");
|
||||||
|
let mut employees: Vec<_> = employees.iter().collect();
|
||||||
|
employees.sort();
|
||||||
|
|
||||||
|
for employee in employees {
|
||||||
|
println!(" {employee}");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => println!("The department {department} does not exist!")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if employees.len() > 0 {
|
||||||
|
println!("All employees:");
|
||||||
|
println!();
|
||||||
|
let mut employees: Vec<_> = employees.iter().collect();
|
||||||
|
employees.sort_by_key(|e| e.0);
|
||||||
|
|
||||||
|
for entry in employees {
|
||||||
|
let department = entry.0;
|
||||||
|
let mut employees: Vec<_> = entry.1.iter().collect();
|
||||||
|
employees.sort();
|
||||||
|
|
||||||
|
println!(" {department}:");
|
||||||
|
|
||||||
|
for employee in employees {
|
||||||
|
println!(" {employee}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("No employees or departments have been added so far.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("The specified command \"{}\" is invalid!", input.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue