From 347c6da0b59106e048106aa05152637624ef808f Mon Sep 17 00:00:00 2001 From: Spxg Date: Fri, 3 May 2024 22:37:23 +0800 Subject: [PATCH] Add problem 0709: To Lower Case --- src/lib.rs | 1 + src/problem_0709_to_lower_case/cheating.rs | 21 ++++++++++++++++ src/problem_0709_to_lower_case/iterative.rs | 27 +++++++++++++++++++++ src/problem_0709_to_lower_case/mod.rs | 19 +++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 src/problem_0709_to_lower_case/cheating.rs create mode 100644 src/problem_0709_to_lower_case/iterative.rs create mode 100644 src/problem_0709_to_lower_case/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 86433e3a..224ce6f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -345,6 +345,7 @@ pub mod problem_0665_non_decreasing_array; pub mod problem_0673_number_of_longest_increasing_subsequence; pub mod problem_0674_longest_continuous_increasing_subsequence; pub mod problem_0677_map_sum_pairs; +pub mod problem_0709_to_lower_case; pub mod problem_0717_1_bit_and_2_bit_characters; pub mod problem_0720_longest_word_in_dictionary; pub mod problem_0738_monotone_increasing_digits; diff --git a/src/problem_0709_to_lower_case/cheating.rs b/src/problem_0709_to_lower_case/cheating.rs new file mode 100644 index 00000000..cf9a6289 --- /dev/null +++ b/src/problem_0709_to_lower_case/cheating.rs @@ -0,0 +1,21 @@ +pub struct Solution; + +impl Solution { + pub fn to_lower_case(s: String) -> String { + s.to_lowercase() + } +} + +impl super::Solution for Solution { + fn to_lower_case(str: String) -> String { + Self::to_lower_case(str) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0709_to_lower_case/iterative.rs b/src/problem_0709_to_lower_case/iterative.rs new file mode 100644 index 00000000..be962ba0 --- /dev/null +++ b/src/problem_0709_to_lower_case/iterative.rs @@ -0,0 +1,27 @@ +pub struct Solution; + +impl Solution { + pub fn to_lower_case(s: String) -> String { + let mut s = s.into_bytes(); + for ch in &mut s { + if *ch >= b'A' && *ch <= b'Z' { + *ch = *ch - b'A' + b'a'; + } + } + String::from_utf8(s).unwrap() + } +} + +impl super::Solution for Solution { + fn to_lower_case(str: String) -> String { + Self::to_lower_case(str) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0709_to_lower_case/mod.rs b/src/problem_0709_to_lower_case/mod.rs new file mode 100644 index 00000000..261d2a3b --- /dev/null +++ b/src/problem_0709_to_lower_case/mod.rs @@ -0,0 +1,19 @@ +pub mod cheating; +pub mod iterative; + +pub trait Solution { + fn to_lower_case(str: String) -> String; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [("Hello", "hello"), ("here", "here"), ("LOVELY", "lovely")]; + + for (str, expected) in test_cases { + assert_eq!(S::to_lower_case(str.to_string()), expected); + } + } +}