Wrote program for Day 31

This commit is contained in:
Mariano Riefolo 2024-08-25 11:07:54 +02:00
parent 5df229b02f
commit 32e0240d0a
5 changed files with 163 additions and 1 deletions

View File

@ -77,7 +77,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
| Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_check_mark: | | Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_check_mark: |
| Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_check_mark: | | Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_check_mark: |
| Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_check_mark: | | Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_check_mark: |
| Day #31 | [The Time In Words](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-31_The-Time-In-Words) | :white_large_square: | | Day #31 | [The Time In Words](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-31_The-Time-In-Words) | :white_check_mark: |
| Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_large_square: | | Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_large_square: |
| Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_large_square: | | Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_large_square: |
| Day #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_large_square: | | Day #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_large_square: |

View File

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

View File

@ -0,0 +1,68 @@
fn number_to_string<'a, 'b>(number: u8) -> Result<&'a str, &'b str> {
match number {
1 => Ok("one"),
2 => Ok("two"),
3 => Ok("three"),
4 => Ok("four"),
5 => Ok("five"),
6 => Ok("six"),
7 => Ok("seven"),
8 => Ok("eight"),
9 => Ok("nine"),
10 => Ok("ten"),
11 => Ok("eleven"),
12 => Ok("twelve"),
13 => Ok("thirteen"),
14 => Ok("fourteen"),
16 => Ok("sixteen"),
17 => Ok("seventeen"),
18 => Ok("eighteen"),
19 => Ok("nineteen"),
20 => Ok("twenty"),
21 => Ok("twentyone"),
22 => Ok("twentytwo"),
23 => Ok("twentythree"),
24 => Ok("twentyfour"),
25 => Ok("twentyfive"),
26 => Ok("twentysix"),
27 => Ok("twentyseven"),
28 => Ok("twentyeight"),
29 => Ok("twentynine"),
_ => Err("Number not found"),
}
}
fn minute_to_string<'a>(minutes: u8) -> Result<String, &'a str> {
match minutes {
0 => Ok(String::from("o' clock")),
1 => Ok(String::from("one minute past")),
15 => Ok(String::from("quarter past")),
30 => Ok(String::from("half past")),
45 => Ok(String::from("quarter to")),
59 => Ok(String::from("one minute to")),
2..30 => Ok(format!("{} minutes past", number_to_string(minutes)?)),
31..59 => Ok(format!("{} minutes to", number_to_string(60 - minutes)?)),
_ => Err("Minute not found"),
}
}
pub fn time_to_string<'a>(hours: u8, minutes: u8) -> Result<String, &'a str> {
if (1..=12).contains(&hours) && (0..60).contains(&minutes) {
let hours = hours + if minutes > 30 { 1 } else { 0 };
if minutes == 0 {
Ok(format!(
"{} {}",
number_to_string(hours)?,
minute_to_string(minutes)?
))
} else {
Ok(format!(
"{} {}",
minute_to_string(minutes)?,
number_to_string(hours)?
))
}
} else {
Err("Time not valid")
}
}

View File

@ -0,0 +1,50 @@
use std::{
io::{self, Write},
process::exit,
};
use day31::time_to_string;
fn main() {
println!("Insert the time in the format hh:mm");
print!("> ");
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 (hours, minutes) = match buffer.split_once(':') {
Some(x) => x,
None => {
eprintln!("':' not found.");
exit(1);
}
};
let hours = match hours.trim().parse() {
Ok(x) => x,
Err(_) => {
eprintln!("Hours is not a valid number");
exit(1);
}
};
let minutes = match minutes.trim().parse() {
Ok(x) => x,
Err(_) => {
eprintln!("Minutes is not a valid number");
exit(1);
}
};
let result = time_to_string(hours, minutes);
match result {
Ok(x) => println!("{x}"),
Err(e) => {
eprintln!("{e}");
exit(1);
}
}
}

View File

@ -0,0 +1,38 @@
#[cfg(test)]
mod exaples {
use day31::time_to_string;
#[test]
fn example1() {
assert_eq!(
time_to_string(5, 47),
Ok(String::from("thirteen minutes to six"))
);
}
#[test]
fn example2() {
assert_eq!(time_to_string(3, 00), Ok(String::from("three o' clock")))
}
#[test]
fn example3() {
assert_eq!(
time_to_string(7, 15),
Ok(String::from("quarter past seven"))
);
}
#[test]
fn example4() {
assert_eq!(time_to_string(5, 30), Ok(String::from("half past five")));
}
#[test]
fn example5() {
assert_eq!(
time_to_string(5, 1),
Ok(String::from("one minute past five"))
);
}
}