Wrote program for Day 29
I suppose that the fourth example returns an error because the input is too short
This commit is contained in:
parent
c83bdbae92
commit
9f259a0850
@ -75,7 +75,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: |
|
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: |
|
||||||
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_check_mark: |
|
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :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 #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_large_square: |
|
| 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_large_square: |
|
| Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :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_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_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 #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_large_square: |
|
||||||
|
6
Week-05/Day-29_Traffic-Light-Checker/day29/Cargo.toml
Normal file
6
Week-05/Day-29_Traffic-Light-Checker/day29/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day29"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
58
Week-05/Day-29_Traffic-Light-Checker/day29/src/lib.rs
Normal file
58
Week-05/Day-29_Traffic-Light-Checker/day29/src/lib.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
pub fn tl_checker(lights: &[char]) -> TLR {
|
||||||
|
if lights.len() < 4 || lights.len() > 15 {
|
||||||
|
return TLR::Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut previous = &'X';
|
||||||
|
|
||||||
|
for light in lights {
|
||||||
|
if !matches!(light, 'R' | 'Y' | 'G' | 'P' | 'C') {
|
||||||
|
return TLR::Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
match previous {
|
||||||
|
'R' => {
|
||||||
|
if !matches!(light, 'G' | 'P' | 'C') {
|
||||||
|
return TLR::Reject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'Y' => {
|
||||||
|
if light != &'R' {
|
||||||
|
return TLR::Reject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'G' => {
|
||||||
|
if light != &'Y' {
|
||||||
|
return TLR::Reject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'P' => {
|
||||||
|
if light != &'R' {
|
||||||
|
return TLR::Reject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'C' => {
|
||||||
|
if light != &'R' {
|
||||||
|
return TLR::Reject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'X' => {
|
||||||
|
if light != &'R' {
|
||||||
|
return TLR::Reject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => return TLR::Error,
|
||||||
|
}
|
||||||
|
|
||||||
|
previous = light;
|
||||||
|
}
|
||||||
|
|
||||||
|
TLR::Accept
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
|
pub enum TLR {
|
||||||
|
Accept,
|
||||||
|
Reject,
|
||||||
|
Error,
|
||||||
|
}
|
23
Week-05/Day-29_Traffic-Light-Checker/day29/src/main.rs
Normal file
23
Week-05/Day-29_Traffic-Light-Checker/day29/src/main.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
use day29::tl_checker;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
print!("Enter the traffic light sequence: ");
|
||||||
|
io::stdout().flush().expect("Failed to flush stdout");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read the sequence");
|
||||||
|
|
||||||
|
let sequence: Vec<char> = buffer.chars().filter(|c| !c.is_whitespace()).collect();
|
||||||
|
|
||||||
|
let result = tl_checker(&sequence);
|
||||||
|
match result {
|
||||||
|
day29::TLR::Accept => println!("The sequence is accepted"),
|
||||||
|
day29::TLR::Reject => println!("The sequence is rejected"),
|
||||||
|
day29::TLR::Error => println!("The sequence is invalid"),
|
||||||
|
}
|
||||||
|
}
|
42
Week-05/Day-29_Traffic-Light-Checker/day29/tests/examples.rs
Normal file
42
Week-05/Day-29_Traffic-Light-Checker/day29/tests/examples.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod examples {
|
||||||
|
use day29::{tl_checker, TLR};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example1() {
|
||||||
|
assert_eq!(
|
||||||
|
tl_checker(&['R', 'G', 'Y', 'R', 'C', 'R', 'G', 'Y', 'R']),
|
||||||
|
TLR::Accept
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example2() {
|
||||||
|
assert_eq!(tl_checker(&['G', 'Y', 'R', 'G', 'Y', 'R']), TLR::Reject);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example3() {
|
||||||
|
assert_eq!(tl_checker(&['R', 'Y', 'G', 'P']), TLR::Reject);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example4() {
|
||||||
|
assert_eq!(tl_checker(&['R', 'G', 'Y']), TLR::Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example5() {
|
||||||
|
assert_eq!(tl_checker(&['X', '8', 'S']), TLR::Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example6() {
|
||||||
|
assert_eq!(
|
||||||
|
tl_checker(&[
|
||||||
|
'R', 'G', 'Y', 'R', 'C', 'R', 'P', 'R', 'G', 'Y', 'R', 'G', 'Y', 'R', 'G', 'Y', 'R'
|
||||||
|
]),
|
||||||
|
TLR::Error
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user