-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from julix-unity/hash-map
Chapter 1 into 8
- Loading branch information
Showing
5 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use rand::Rng; | ||
use std::{cmp::Ordering, io}; | ||
|
||
pub fn guessing_game() { | ||
println!("Guess the number!"); | ||
|
||
let secret_number = rand::thread_rng().gen_range(1..=10); | ||
|
||
loop { | ||
println!("Please input your guess."); | ||
|
||
let mut guess = String::new(); | ||
|
||
io::stdin() | ||
.read_line(&mut guess) | ||
.expect("Failed to read line"); | ||
|
||
let guess: u32 = match guess.trim().parse() { | ||
Ok(num) => num, | ||
Err(_) => { | ||
println!("That's not a number, try again!"); | ||
continue; | ||
}, | ||
}; | ||
|
||
println!("You guessed: {guess}"); | ||
|
||
match guess.cmp(&secret_number) { | ||
Ordering::Less => println!("Too small!"), | ||
Ordering::Greater => println!("Too big!"), | ||
Ordering::Equal => { | ||
println!("You win!"); | ||
break; | ||
}, | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,21 @@ | ||
fn main() { | ||
println!("Hello, World!"); | ||
use guessing_game::guessing_game; | ||
|
||
pub mod guessing_game; | ||
|
||
use median_and_mode::{get_median, get_mode}; | ||
|
||
let hello: Vec<i32> = (0..10).collect(); | ||
pub mod median_and_mode; | ||
|
||
fn do_stuff(val: &Vec<i32>) { | ||
println!("{}", val.len()); // this is a MACRO! | ||
|
||
const RUN_GUESSING_GAME: bool = false; | ||
|
||
fn main() { | ||
if RUN_GUESSING_GAME { | ||
guessing_game(); | ||
} | ||
|
||
do_stuff(&hello); | ||
let vector = vec![1,2,3,4,2, 5, 5, 5,]; | ||
let median = get_median(vector.clone()); | ||
let mode = get_mode(vector); | ||
println!("Median: {median} and Mode: {:?}", {mode}); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Given a list of integers, use a vector and return the median (when sorted, the value in the middle position) | ||
* and mode (the value that occurs most often; a hash map will be helpful here) of the list | ||
*/ | ||
|
||
use std::collections::HashMap; | ||
|
||
pub fn get_median (mut list: Vec<i32>) -> f32 { | ||
|
||
// median | ||
list.sort(); | ||
let length = list.len(); | ||
let is_even = length % 2 == 0; | ||
|
||
match is_even { | ||
true => (list[length / 2] as f32 + list[(length / 2) - 1] as f32) / 2.0, | ||
false => list[length / 2] as f32 | ||
} | ||
} | ||
|
||
pub fn get_mode (list: Vec<i32>) -> Vec<i32> { | ||
let mut num_map = HashMap::new(); | ||
|
||
// populate map | ||
for &number in &list { | ||
let count = num_map.entry(number).or_insert(0); | ||
*count += 1 | ||
} | ||
|
||
// Find the highest count | ||
let max_count = num_map.values().cloned().max().unwrap_or(0); | ||
|
||
// Collect all numbers with the highest count | ||
num_map.into_iter() | ||
.filter(|&(_, count)| count == max_count) | ||
.map(|(num, _)| num) | ||
.collect() | ||
} |