Wrote program for Day 59
This commit is contained in:
parent
e1e65e718a
commit
4ed0deba71
@ -105,7 +105,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_check_mark: |
|
| Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_check_mark: |
|
||||||
| Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_check_mark: |
|
| Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_check_mark: |
|
||||||
| Day #58 | [Create A Dice Roller](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-58_Create-A-Dice-Roller) | :white_check_mark: |
|
| Day #58 | [Create A Dice Roller](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-58_Create-A-Dice-Roller) | :white_check_mark: |
|
||||||
| Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_large_square: |
|
| Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_check_mark: |
|
||||||
| Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_large_square: |
|
| Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_large_square: |
|
||||||
| Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_large_square: |
|
| Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_large_square: |
|
||||||
| Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_large_square: |
|
| Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_large_square: |
|
||||||
|
6
Week-09/Day-59_Perfectly-Balanced/day59/Cargo.toml
Normal file
6
Week-09/Day-59_Perfectly-Balanced/day59/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day59"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
13
Week-09/Day-59_Perfectly-Balanced/day59/src/lib.rs
Normal file
13
Week-09/Day-59_Perfectly-Balanced/day59/src/lib.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub fn balanced_bonus(string: &str) -> bool {
|
||||||
|
let mut counts = HashMap::new();
|
||||||
|
for c in string.chars() {
|
||||||
|
counts.entry(c).and_modify(|e| *e += 1usize).or_insert(1);
|
||||||
|
}
|
||||||
|
if string.is_empty() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
let idx = string.chars().next().unwrap();
|
||||||
|
counts.iter().all(|x| *x.1 == counts[&idx])
|
||||||
|
}
|
14
Week-09/Day-59_Perfectly-Balanced/day59/src/main.rs
Normal file
14
Week-09/Day-59_Perfectly-Balanced/day59/src/main.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
use day59::balanced_bonus;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
print!("Enter a string to check if it is balanced: ");
|
||||||
|
io::stdout().flush().unwrap();
|
||||||
|
let mut input = String::new();
|
||||||
|
std::io::stdin().read_line(&mut input).unwrap();
|
||||||
|
match balanced_bonus(input.trim()) {
|
||||||
|
true => println!("The string is balanced."),
|
||||||
|
false => println!("The string is not balanced."),
|
||||||
|
}
|
||||||
|
}
|
49
Week-09/Day-59_Perfectly-Balanced/day59/tests/examples.rs
Normal file
49
Week-09/Day-59_Perfectly-Balanced/day59/tests/examples.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod examples {
|
||||||
|
use day59::balanced_bonus;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example1() {
|
||||||
|
assert!(balanced_bonus("xxxyyyzzz"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example2() {
|
||||||
|
assert!(balanced_bonus("abccbaabccba"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example3() {
|
||||||
|
assert!(!balanced_bonus("xxxyyyzzzz"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example4() {
|
||||||
|
assert!(balanced_bonus("abcdefghijklmnopqrstuvwxyz"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example5() {
|
||||||
|
assert!(!balanced_bonus("pqq"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example6() {
|
||||||
|
assert!(!balanced_bonus("fdedfdeffeddefeeeefddf"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example7() {
|
||||||
|
assert!(balanced_bonus("www"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example8() {
|
||||||
|
assert!(balanced_bonus("x"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example9() {
|
||||||
|
assert!(balanced_bonus(""));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user