Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Last active June 18, 2019 12:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinknopoff/db4222e43085099d514a75349e2de778 to your computer and use it in GitHub Desktop.
Save dustinknopoff/db4222e43085099d514a75349e2de778 to your computer and use it in GitHub Desktop.
Is the string sorted?
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