Skip to content

Instantly share code, notes, and snippets.

@zoeisnowooze
Created November 29, 2021 18:33
Show Gist options
  • Save zoeisnowooze/2d4de576560cd1f340956aca424a94b0 to your computer and use it in GitHub Desktop.
Save zoeisnowooze/2d4de576560cd1f340956aca424a94b0 to your computer and use it in GitHub Desktop.
#![allow(unused)]
extern crate itertools;
use itertools::Itertools;
fn phone_letter(number: &str) -> Vec<String> {
number
.chars()
.filter_map(|digit| match digit {
'2' => Some(vec!['a', 'b', 'c']),
'3' => Some(vec!['d', 'e', 'f']),
'4' => Some(vec!['g', 'h', 'i']),
'5' => Some(vec!['j', 'k', 'l']),
'6' => Some(vec!['m', 'n', 'o']),
'7' => Some(vec!['p', 'q', 'r', 's']),
'8' => Some(vec!['t', 'u', 'v']),
'9' => Some(vec!['w', 'x', 'y', 'z']),
_ => None,
})
.multi_cartesian_product()
.map(|chars| chars.iter().collect::<String>())
.collect()
}
#[test]
fn single_digit() {
assert_eq!(phone_letter("9"), vec!["w", "x", "y", "z"]);
}
#[test]
fn multiple_digits() {
assert_eq!(
phone_letter("23"),
vec!["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment