-
Notifications
You must be signed in to change notification settings - Fork 18
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 #3 from dcodesdev/dev
new challenge: median and mode
- Loading branch information
Showing
8 changed files
with
160 additions
and
0 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
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,6 @@ | ||
[package] | ||
name = "median-and-mod" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,64 @@ | ||
## Vectors | ||
|
||
Vectors in Rust are a collection type that can store multiple values of the same type. They are similar to **arrays in JavaScript** and **lists in Python** but with some additional features, they are stored on the heap and they can grow or shrink in size. | ||
|
||
In this challenge you're asked to implement two functions that calculate the **median and mode** of a list of integers. | ||
|
||
Read the definitions of each **Median** and **Mode** below to understand what you need to implement. | ||
|
||
### Median | ||
|
||
The median is the middle value of a list when it is ordered by size. If the list has an even number of elements, the median is the average of the two middle numbers. | ||
|
||
Here's an example: | ||
|
||
```rust | ||
let mut numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
let median = median(&mut numbers); | ||
assert_eq!(median, 5.0); | ||
``` | ||
|
||
Or if the number of elements is even: | ||
|
||
```rust | ||
let mut numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | ||
let median = median(&mut numbers); | ||
assert_eq!(median, 5.5); | ||
``` | ||
|
||
You can implement the function in the following steps: | ||
|
||
- Sort the list of numbers. | ||
- If the number of elements is odd, return the middle element. | ||
- If the number of elements is even, return the average of the two middle elements. | ||
|
||
### Mode | ||
|
||
The mode is the number that appears most frequently in a list, if there are multiple numbers that appear the same number of times, return all of them in a vector. | ||
|
||
Here's an example: | ||
|
||
```rust | ||
let numbers = vec![1, 1, 1, 1, 2] | ||
let mode = mode(&numbers); | ||
assert_eq!(mode, vec![1]); | ||
``` | ||
|
||
The number `1` appears four times, which is more than any other number in the list. | ||
|
||
> **Important**: You must sort the list of numbers before returning the modes list. | ||
### Hints | ||
|
||
- You can use the `sort` method to sort a vector. | ||
- You can use the `entry` method to insert a value into a hash map if it doesn't exist or update it if it does. | ||
- When looping through a vector, you can dereference the value using `&` to get the value instead of a reference to it. For example: | ||
|
||
```rust | ||
let numbers = vec![1, 2, 3, 4, 5]; | ||
for &number in &numbers { | ||
println!("{}", number); | ||
} | ||
``` | ||
|
||
- Store the maximum frequency and the number that appears most frequently in a `HashMap<i32, i32>` and update it as you iterate through the list of numbers, and finally return the list of numbers that appears most frequently. |
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,30 @@ | ||
mod tests; | ||
|
||
use std::collections::HashMap; | ||
|
||
pub fn median(numbers: &mut Vec<i32>) -> f32 { | ||
numbers.sort(); | ||
let mid = numbers.len() / 2; | ||
if numbers.len() % 2 == 0 { | ||
(numbers[mid - 1] as f32 + numbers[mid] as f32) / 2.0 | ||
} else { | ||
numbers[mid] as f32 | ||
} | ||
} | ||
|
||
pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { | ||
let mut occurrences = HashMap::new(); | ||
for &number in numbers { | ||
*occurrences.entry(number).or_insert(0) += 1; | ||
} | ||
|
||
let max_count = occurrences.values().max().unwrap_or(&0); | ||
let mut modes: Vec<i32> = occurrences | ||
.iter() | ||
.filter(|&(_, &count)| count == *max_count) | ||
.map(|(&number, _)| number) | ||
.collect(); | ||
|
||
modes.sort(); | ||
modes | ||
} |
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,9 @@ | ||
use std::collections::HashMap; | ||
|
||
pub fn median(numbers: &mut Vec<i32>) -> f32 { | ||
// TODO: Implement logic here to return the median of the list | ||
} | ||
|
||
pub fn mode(numbers: &Vec<i32>) -> Vec<i32> { | ||
// TODO: Implement logic here to return the mode of the list | ||
} |
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,34 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::{median, mode}; | ||
|
||
#[test] | ||
fn test_median() { | ||
let mut numbers = vec![1, 2, 2, 3, 4]; | ||
assert_eq!(median(&mut numbers), 2.0); | ||
|
||
let mut numbers = vec![6, 1, 2, 3, 4, 5]; | ||
assert_eq!(median(&mut numbers), 3.5); | ||
|
||
let mut numbers = vec![1, 2, 3, 4, 5, 6, 7]; | ||
assert_eq!(median(&mut numbers), 4.0); | ||
|
||
let mut numbers = vec![123, 456, 789, 101112, 131415]; | ||
assert_eq!(median(&mut numbers), 789.0); | ||
} | ||
|
||
#[test] | ||
fn test_mode() { | ||
let numbers = vec![1, 2, 2, 2, 3, 4]; | ||
assert_eq!(mode(&numbers), vec![2]); | ||
|
||
let numbers = vec![1, 3, 3, 6, 7, 8, 9]; | ||
assert_eq!(mode(&numbers), vec![3]); | ||
|
||
let numbers = vec![2, 2, 2, 2, 2, 2, 2, 100, 100, 100, 100, 100, 100, 100, 100]; | ||
assert_eq!(mode(&numbers), vec![100]); | ||
|
||
let numbers = vec![1, 1, 2, 2]; | ||
assert_eq!(mode(&numbers), vec![1, 2]); | ||
} | ||
} |