Created
June 21, 2022 01:24
-
-
Save maczniak/92f4b5230887573ba31b620de97c82d8 to your computer and use it in GitHub Desktop.
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 sorted_count<T: std::cmp::PartialEq + Copy>(entries: &Vec<T>) -> Vec<(T, u32)> { | |
let mut result = Vec::new(); | |
let mut previous = &entries[0]; | |
let mut count : u32 = 0; | |
for entry in entries { | |
if entry == previous { | |
count += 1; | |
} else { | |
result.push((*previous, count)); | |
previous = entry; | |
count = 1; | |
} | |
} | |
result.push((entries[entries.len() - 1], count)); | |
result | |
} | |
fn main() { | |
let mut integers = vec![8, 4, 8, 1, 3, 3, 3, 5, 4, 10]; | |
println!("input: {:?}", integers); | |
integers.sort(); | |
let len = integers.len(); | |
let median: f64 = if len % 2 == 0 { | |
((integers[len / 2 - 1] + integers[len / 2]) as f64) / 2.0 | |
} else { | |
integers[len / 2] as f64 | |
}; | |
// let mut occurrence : HashMap<int, int> = HashMap::new(); | |
let mut total = 0; | |
let mut most_popular = integers[0]; | |
let mut occurrence = 1; | |
for (i, count) in sorted_count(&integers) { | |
if occurrence < count { | |
most_popular = i; | |
occurrence = count; | |
} | |
total += i * count; | |
} | |
println!("mean: {}", (total as f64) / (len as f64)); | |
println!("median: {}", median); | |
println!("mode: {}", most_popular); | |
} |
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
fn pig_latin(w: &str) -> String { | |
let word = String::from(w); | |
let mut iter = word.chars(); | |
let first_char = iter.next().unwrap(); | |
let result = match first_char { | |
'a' | 'e' | 'i' | 'o' | 'u' | | |
'A' | 'E' | 'I' | 'O' | 'U' => | |
format!("{}-hay", word), | |
_ => | |
format!("{}-{}ay", iter.as_str(), first_char) | |
}; | |
result | |
} | |
fn main() { | |
println!("first -> {}", pig_latin("first")); | |
println!("apple -> {}", pig_latin("apple")); | |
} |
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
// question 1. `let persons` vs `let mut teams` | |
// question 2. &str vs String vs teams' &String | |
use std::collections::HashMap; | |
fn main() { | |
let mut directory : HashMap<String, Vec<String>> = HashMap::new(); | |
let inputs = vec![ | |
"Add Sally to Engineering", | |
"Add Amir to Sales", | |
"Add Jin to Engineering", | |
"Add Hee to Engineering", | |
"Show Engineering", | |
"Show All" | |
]; | |
for input in &inputs { | |
println!("> {}", input); | |
let words : Vec<&str> = input.split_whitespace().collect(); | |
if words.len() == 4 && words[0] == "Add" && words[2] == "to" { | |
// Add person to team | |
let persons = directory.entry(String::from(words[3])).or_insert(Vec::new()); | |
persons.push(String::from(words[1])); | |
persons.sort(); | |
continue | |
} else if words.len() == 2 && words[0] == "Show" { | |
if words[1] == "All" { | |
// Show All | |
let mut teams = Vec::from_iter(directory.keys()); | |
teams.sort(); | |
for team in teams { | |
println!("{} team:", team); | |
for person in directory.get(team).unwrap() { | |
println!("\t{}", person) | |
} | |
} | |
} else { | |
// Show team | |
if directory.get(words[1]) == None { | |
println!("there is no {} team", words[1]); | |
continue | |
} | |
for person in directory.get(words[1]).unwrap() { | |
println!("{}", person) | |
} | |
} | |
continue | |
} | |
println!("unrecognizable input: {}", input) | |
} | |
} |
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::io::stdin; | |
let mut input = String::new(); | |
stdin().read_line(&mut input).expect("Unable to Read standard Input"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment