Last active
June 18, 2019 12:28
-
-
Save dustinknopoff/db4222e43085099d514a75349e2de778 to your computer and use it in GitHub Desktop.
Is the string sorted?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn is_sorted(input: &str) -> bool { | |
let input_iter: Vec<_> = input.chars().collect(); | |
let initial = input_iter[0]; | |
input | |
.chars() | |
.fold(Some(initial), |acc, current| { | |
if acc != None { | |
if Some(current) >= acc { | |
return Some(current) | |
} else { | |
return None | |
} | |
} | |
acc | |
}) | |
.is_some() | |
} | |
fn is_sorted_ignore_case(input: &str) -> bool { | |
let input = input.to_lowercase(); | |
is_sorted(&input) | |
} | |
fn main() { | |
dbg!(is_sorted("abc")); | |
dbg!(is_sorted("bac")); | |
dbg!(is_sorted("023BCDabc")); | |
dbg!(is_sorted_ignore_case("abBcCD")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment