blob: ba63d643236b9cea0da55830b58462d7467e029f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
use std::{{collections::HashMap as hm, io}};
use rand::Rng;
fn main() {
median();
pig_latin(&"appearance is the best".to_string());
add_emp();
}
fn median() {
// generate a list of number
let mut arr = Vec::new();
for _x in 0..100 {
let p = rand::thread_rng().gen_range(1..=10);
arr.push(p);
}
// count each number's appearance
let mut num_hash = hm::new();
for x in &arr {
let count = num_hash.entry(x).or_insert(0);
*count += 1;
}
// get most appearance
let mut key = 0;
let mut ret = 0;
for (k,v) in num_hash.iter() {
if *v > ret {
key = **k;
ret = *v;
}
}
println!("most appearance: {key} {ret}");
}
fn pig_latin(st: &String) {
let vowel = ['a', 'e', 'i', 'o', 'u'];
for w in st.split_whitespace() {
for v in vowel {
if w.starts_with(v) {
println!("{:?}",format!("{w}-hay"));
break;
} else {
let pl = w.chars().nth(0).unwrap();
println!("{:?}",format!("{w}-{pl}ay"));
break;
}
}
}
}
fn add_emp() {
println!("Employee interface");
let mut hash: hm<String, Vec<String>> = hm::new();
loop {
let mut rd = String::new();
println!("enter name: ");
io::stdin()
.read_line(&mut rd)
.expect("failed to read line");
if rd.trim() == "q" {
break;
}
let mut wd = String::new();
println!("enter department: ");
io::stdin()
.read_line(&mut wd)
.expect("failed to read line");
if wd.trim() == "q" {
break;
}
hash.entry(wd).or_insert_with(Vec::new).push((*rd.trim()).to_string());
}
for (k,v) in &hash {
println!("{k} {:?}", v);
}
}
|