Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Last active May 29, 2023 18:06
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 keturiosakys/dbf029bd753849f93646009d22910470 to your computer and use it in GitHub Desktop.
Save keturiosakys/dbf029bd753849f93646009d22910470 to your computer and use it in GitHub Desktop.
All words you can type in one row
static KEYBOARD_ROWS: [&str; 3] = ["qwertyuiop", "asdfghjkl", "zxcvbnm"];
fn main() {
let words = vec!["candy", "fart", "pop", "Zelda", "flag", "typewriter"];
let res = one_row(words);
println!("{:?}", res);
}
fn one_row(words: Vec<&str>) -> Vec<&str> {
return words
.into_iter()
.filter(|word| {
KEYBOARD_ROWS
.iter()
.find(|row| {
word.to_lowercase()
.chars()
.all(|input_char| row.contains(input_char))
})
.is_some()
})
.collect();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_one_row() {
assert_eq!(
vec!["pop", "flag", "typewriter"],
one_row(vec!["candy", "fart", "pop", "Zelda", "flag", "typewriter"])
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment