Wrote program for Day 41
This commit is contained in:
parent
a7e7dab83e
commit
dc46978ac4
@ -87,7 +87,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
||||
| Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_check_mark: |
|
||||
| Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_check_mark: |
|
||||
| Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_check_mark: |
|
||||
| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_large_square: |
|
||||
| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_check_mark: |
|
||||
| Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: |
|
||||
| Day #43 | [Area Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-43_Area-Of-A-Triangle) | :white_large_square: |
|
||||
| Day #44 | [Maximum Edge Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-44_Maximum-Edge-Of-A-Triangle) | :white_large_square: |
|
||||
|
6
Week-06/Day-41_Sales-By-Match/day41/Cargo.toml
Normal file
6
Week-06/Day-41_Sales-By-Match/day41/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day41"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
18
Week-06/Day-41_Sales-By-Match/day41/src/lib.rs
Normal file
18
Week-06/Day-41_Sales-By-Match/day41/src/lib.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn socks_match(socks: &[usize]) -> usize {
|
||||
let mut count: HashMap<&usize, usize> = HashMap::new();
|
||||
let mut result = 0;
|
||||
|
||||
for sock in socks {
|
||||
count.entry(sock).and_modify(|x| *x += 1).or_default();
|
||||
if count[sock] == 2 {
|
||||
result += 1;
|
||||
if let Some(x) = count.get_mut(&sock) {
|
||||
*x = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
30
Week-06/Day-41_Sales-By-Match/day41/src/main.rs
Normal file
30
Week-06/Day-41_Sales-By-Match/day41/src/main.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
process::exit,
|
||||
};
|
||||
|
||||
use day41::socks_match;
|
||||
|
||||
fn main() {
|
||||
print!("Please enter the colors of the socks as a list of numbers, separated by spaces: ");
|
||||
io::stdout().flush().expect("Failed to flush stdout");
|
||||
|
||||
let mut buffer = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut buffer)
|
||||
.expect("Failed to read line");
|
||||
|
||||
let socks: Vec<usize> = buffer
|
||||
.split_whitespace()
|
||||
.map(|x| match x.parse() {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
eprintln!("{e}");
|
||||
exit(1);
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let count = socks_match(&socks);
|
||||
println!("The number of pairs is {count}.");
|
||||
}
|
9
Week-06/Day-41_Sales-By-Match/day41/tests/example.rs
Normal file
9
Week-06/Day-41_Sales-By-Match/day41/tests/example.rs
Normal file
@ -0,0 +1,9 @@
|
||||
#[cfg(test)]
|
||||
mod example {
|
||||
use day41::socks_match;
|
||||
|
||||
#[test]
|
||||
fn test_example() {
|
||||
assert_eq!(socks_match(&[1, 2, 1, 2, 1, 3, 2]), 2);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user