Skip to content

Commit

Permalink
Merge pull request #1 from julix-unity/hash-map
Browse files Browse the repository at this point in the history
Chapter 1 into 8
  • Loading branch information
julix-unity authored Feb 19, 2024
2 parents 2bae42d + 588c9e4 commit 994584a
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 6 deletions.
75 changes: 75 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rand = "0.8.5"
37 changes: 37 additions & 0 deletions src/guessing_game.rs
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;
},
}
}
}
22 changes: 16 additions & 6 deletions src/main.rs
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});
}
38 changes: 38 additions & 0 deletions src/median_and_mode.rs
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()
}

0 comments on commit 994584a

Please sign in to comment.