Skip to content

Commit

Permalink
Add problem 0709: To Lower Case
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed May 3, 2024
1 parent 01d0c41 commit 347c6da
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/problem_0709_to_lower_case/cheating.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
27 changes: 27 additions & 0 deletions src/problem_0709_to_lower_case/iterative.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
19 changes: 19 additions & 0 deletions src/problem_0709_to_lower_case/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}

0 comments on commit 347c6da

Please sign in to comment.