Wrote program for Day 59

This commit is contained in:
Mariano Riefolo 2024-09-22 12:38:48 +02:00
parent e1e65e718a
commit 4ed0deba71
5 changed files with 83 additions and 1 deletions

View File

@ -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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day59"
version = "0.1.0"
edition = "2021"
[dependencies]

View 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])
}

View 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."),
}
}

View 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(""));
}
}